You are here

public function TreeRebuilder::getRebuildTasks in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/TreeRebuilder.php \Drupal\entity_hierarchy\Storage\TreeRebuilder::getRebuildTasks()

Gets rebuild tasks suitable for usage with batch_set().

Parameters

string $field_name: Field name to rebuild.

string $entity_type_id: Entity Type to rebuild.

Return value

array Batch definition.

File

src/Storage/TreeRebuilder.php, line 42

Class

TreeRebuilder
Defines a class for rebuilding the tree.

Namespace

Drupal\entity_hierarchy\Storage

Code

public function getRebuildTasks($field_name, $entity_type_id) {
  $batch = [
    'title' => new TranslatableMarkup('Rebuilding tree for field @field, @entity_type_id ...', [
      '@field' => $field_name,
      '@entity_type_id' => $entity_type_id,
    ]),
    'operations' => [
      [
        [
          static::class,
          'removeTable',
        ],
        [
          $field_name,
          $entity_type_id,
        ],
      ],
    ],
    'finished' => [
      static::class,
      'batchFinished',
    ],
  ];
  $entityType = $this->entityTypeManager
    ->getDefinition($entity_type_id);
  $idKey = $entityType
    ->getKey('id');
  $query = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->getAggregateQuery()
    ->groupBy("{$field_name}.target_id")
    ->groupBy("{$field_name}.weight")
    ->groupBy($idKey)
    ->sort("{$field_name}.target_id")
    ->sort("{$field_name}.weight")
    ->exists($field_name)
    ->accessCheck(FALSE);
  $records = $query
    ->execute();
  $sorted = $this
    ->treeSort($field_name, $records, $idKey);
  foreach ($sorted as $entity_id => $entry) {
    $batch['operations'][] = [
      [
        static::class,
        'rebuildTree',
      ],
      [
        $field_name,
        $entity_type_id,
        $entity_id,
      ],
    ];
  }
  return $batch;
}