You are here

public function EntityWarmer::buildIdsBatch in Warmer 2.x

Same name and namespace in other branches
  1. 8 modules/warmer_entity/src/Plugin/warmer/EntityWarmer.php \Drupal\warmer_entity\Plugin\warmer\EntityWarmer::buildIdsBatch()

TODO: This is a naive implementation.

Overrides WarmerInterface::buildIdsBatch

File

modules/warmer_entity/src/Plugin/warmer/EntityWarmer.php, line 124

Class

EntityWarmer
The cache warmer for the built-in entity cache.

Namespace

Drupal\warmer_entity\Plugin\warmer

Code

public function buildIdsBatch($cursor) {
  $configuration = $this
    ->getConfiguration();
  if (empty($this->iids) && !empty($configuration['entity_types'])) {
    $entity_bundle_pairs = array_filter(array_values($configuration['entity_types']));
    sort($entity_bundle_pairs);
    $this->iids = array_reduce($entity_bundle_pairs, function ($iids, $entity_bundle_pair) {
      list($entity_type_id, $bundle) = explode(':', $entity_bundle_pair);
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      $bundle_key = $entity_type
        ->getKey('bundle');
      $id_key = $entity_type
        ->getKey('id');
      $query = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->getQuery();
      if (!empty($id_key)) {
        $query
          ->sort($id_key);
      }
      if (!empty($bundle_key)) {
        $query
          ->condition($bundle_key, $bundle);
      }
      $results = $query
        ->execute();
      $entity_ids = array_filter((array) array_values($results));
      $iids = array_merge($iids, array_map(function ($id) use ($entity_type_id) {
        return sprintf('%s:%s', $entity_type_id, $id);
      }, $entity_ids));
      return $iids;
    }, []);
  }
  $cursor_position = is_null($cursor) ? -1 : array_search($cursor, $this->iids);
  if ($cursor_position === FALSE) {
    return [];
  }
  return array_slice($this->iids, $cursor_position + 1, (int) $this
    ->getBatchSize());
}