You are here

function taxonomy_entity_index_reindex_entity_type in Taxonomy Entity Index 8

Same name and namespace in other branches
  1. 7 taxonomy_entity_index.module \taxonomy_entity_index_reindex_entity_type()

Batch callback; re-index all the terms for a given entity type.

2 string references to 'taxonomy_entity_index_reindex_entity_type'
drush_taxonomy_entity_index_rebuild in ./taxonomy_entity_index.drush.inc
Rebuilds the taxonomy entity index.
TaxonomyEntityIndexAdminReindexForm::submitForm in src/Form/TaxonomyEntityIndexAdminReindexForm.php
Form submission handler.

File

./taxonomy_entity_index.module, line 200
Module file for taxonomy_entity_index.

Code

function taxonomy_entity_index_reindex_entity_type($entity_type_id, &$context) {
  $entity_type_manger = \Drupal::entityTypeManager();
  $entity_type = $entity_type_manger
    ->getDefinition($entity_type_id);
  $bundle_key = $entity_type
    ->getKey('bundle');
  if (empty($context['sandbox'])) {
    $context['sandbox'] = [];
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['bundles'] = array_keys(taxonomy_entity_index_get_taxonomy_field_names($entity_type_id));
    $context['sandbox']['entity_key_id'] = $entity_type
      ->getKey('id');

    // Clear out any records for this type.
    \Drupal::database()
      ->delete('taxonomy_entity_index')
      ->condition('entity_type', $entity_type_id)
      ->execute();

    // Calculate the maximum number of entities.
    if (!empty($context['sandbox']['bundles'])) {
      $query = \Drupal::entityQuery($entity_type_id);
      if ($bundle_key) {
        $query
          ->condition($bundle_key, $context['sandbox']['bundles'], 'IN');
      }
      $query
        ->condition($context['sandbox']['entity_key_id'], 0, '>');
      $query
        ->count();
      $context['sandbox']['max'] = (int) $query
        ->execute();
    }
    if (empty($context['sandbox']['max'])) {
      $context['finished'] = TRUE;
      return;
    }
  }
  $entity_id_key = $context['sandbox']['entity_key_id'];
  $limit = 25;
  $query = \Drupal::entityQuery($entity_type_id);
  if ($bundle_key) {
    $query
      ->condition($bundle_key, $context['sandbox']['bundles'], 'IN');
  }
  $query
    ->condition($entity_id_key, $context['sandbox']['current_id'], '>');
  $query
    ->sort($entity_id_key, 'ASC');
  $query
    ->range(0, $limit);
  $results = $query
    ->execute();
  $entities = $entity_type_manger
    ->getStorage($entity_type_id)
    ->loadMultiple($results);
  foreach ($entities as $id => $entity) {
    taxonomy_entity_index_entity_update($entity);
    $context['sandbox']['current_id'] = $id;
    $context['message'] = t('Updating taxonomy entity index data for @type @id.', [
      '@type' => $entity_type_id,
      '@id' => $id,
    ]);
  }

  // In case any of the entities fail to load, increase the progress by the
  // stub entity count.
  $context['sandbox']['progress'] += count($results);
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] >= $context['sandbox']['max'];
  }
}