You are here

public function BatchUpdateForm::generateBatch in Entity Usage 8.3

Same name and namespace in other branches
  1. 8 src/Form/BatchUpdateForm.php \Drupal\entity_usage\Form\BatchUpdateForm::generateBatch()

Create a batch to process the entity types in bulk.

Return value

array The batch array.

1 call to BatchUpdateForm::generateBatch()
BatchUpdateForm::submitForm in src/Form/BatchUpdateForm.php
Form submission handler.

File

src/Form/BatchUpdateForm.php, line 85

Class

BatchUpdateForm
Form to launch batch tracking of existing entities.

Namespace

Drupal\entity_usage\Form

Code

public function generateBatch() {
  $operations = [];
  $to_track = \Drupal::config('entity_usage.settings')
    ->get('track_enabled_source_entity_types');
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {

    // Only look for entities enabled for tracking on the settings form.
    $track_this_entity_type = FALSE;
    if (!is_array($to_track) && $entity_type
      ->entityClassImplements('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {

      // When no settings are defined, track all content entities by default,
      // except for Files and Users.
      if (!in_array($entity_type_id, [
        'file',
        'user',
      ])) {
        $track_this_entity_type = TRUE;
      }
    }
    elseif (is_array($to_track) && in_array($entity_type_id, $to_track, TRUE)) {
      $track_this_entity_type = TRUE;
    }
    if ($track_this_entity_type) {
      $operations[] = [
        'Drupal\\entity_usage\\Form\\BatchUpdateForm::updateSourcesBatchWorker',
        [
          $entity_type_id,
        ],
      ];
    }
  }
  $batch = [
    'operations' => $operations,
    'finished' => 'Drupal\\entity_usage\\Form\\BatchUpdateForm::batchFinished',
    'title' => $this
      ->t('Updating entity usage statistics.'),
    'progress_message' => $this
      ->t('Processed @current of @total entity types.'),
    'error_message' => $this
      ->t('This batch encountered an error.'),
  ];
  return $batch;
}