You are here

protected function EntityUsageCommands::generateQueueItems in Entity Usage 8.2

Populate items to be tracked into the EU tracking queue.

Parameters

string $entity_type_id: The entity type machine name

bool $multi_pass: Whether this is operating in multi_pass mode. If true, this will store in Drupal state the ID of the last queue item created, and if something interrupts the execution, next time this is called in multi_pass mode, this will resume creating items from the last ID we stopped earlier, instead of creating from scratch. Defaults to FALSE.

1 call to EntityUsageCommands::generateQueueItems()
EntityUsageCommands::recreate in src/Commands/EntityUsageCommands.php
Recreate all entity usage statistics.

File

src/Commands/EntityUsageCommands.php, line 124

Class

EntityUsageCommands
Entity Usage drush commands.

Namespace

Drupal\entity_usage\Commands

Code

protected function generateQueueItems($entity_type_id, $multi_pass = FALSE) {
  $queue = \Drupal::queue('entity_usage_regenerate_queue');
  if ($multi_pass) {
    $multi_pass_from_state = \Drupal::state()
      ->get('entity_usage.multi_pass', []);
    if (!empty($multi_pass_from_state[$entity_type_id])) {
      $current_id = $multi_pass_from_state[$entity_type_id];
    }
    else {
      $current_id = 0;
    }
  }

  // Delete current usage statistics for these entities if we are starting
  // for this entity_type.
  if (empty($current_id)) {
    \Drupal::service('entity_usage.usage')
      ->bulkDeleteSources($entity_type_id);
  }
  $storage = $this->entityTypeManager
    ->getStorage($entity_type_id);
  $entity_type = $storage
    ->getEntityType();
  if ($entity_type
    ->isRevisionable()) {
    $revision_key = $entity_type
      ->getKey('revision');
    $query = $storage
      ->getQuery()
      ->allRevisions()
      ->sort($revision_key, 'ASC')
      ->accessCheck(FALSE);
    if ($multi_pass) {
      $query
        ->condition($revision_key, $current_id, '>');
    }
    $result = $query
      ->execute();
    foreach ($result as $revision_id => $id) {
      $queue
        ->createItem([
        'entity_type' => $entity_type_id,
        'entity_revision_id' => $revision_id,
      ]);
      if ($multi_pass) {
        $multi_pass_from_state[$entity_type_id] = $revision_id;
        \Drupal::state()
          ->set('entity_usage.multi_pass', $multi_pass_from_state);
      }
    }
  }
  else {
    $id_key = $entity_type
      ->getKey('id');
    $query = $storage
      ->getQuery()
      ->sort($id_key, 'ASC')
      ->accessCheck(FALSE);
    if ($multi_pass) {
      $query
        ->condition($id_key, $current_id, '>');
    }
    $result = $query
      ->execute();
    foreach ($result as $id) {
      $queue
        ->createItem([
        'entity_type' => $entity_type_id,
        'entity_id' => $id,
      ]);
      if ($multi_pass) {
        $multi_pass_from_state[$entity_type_id] = $id;
        \Drupal::state()
          ->set('entity_usage.multi_pass', $multi_pass_from_state);
      }
    }
  }
}