You are here

public function SearchService::entityMarkForReindexMultiple in Synonyms 2.0.x

Same name and namespace in other branches
  1. 8 synonyms_search/src/SynonymsService/Behavior/SearchService.php \Drupal\synonyms_search\SynonymsService\Behavior\SearchService::entityMarkForReindexMultiple()

Mark all search index dependent on given entities for reindexing.

Parameters

array $entity_ids: Entity IDs whose dependent search index should be marked for reindexing.

string $entity_type: Entity type of the give entity IDs.

1 call to SearchService::entityMarkForReindexMultiple()
SearchService::entityMarkForReindex in modules/synonyms_search/src/SynonymsService/Behavior/SearchService.php
Mark all search index dependent on a given entity for reindexing.

File

modules/synonyms_search/src/SynonymsService/Behavior/SearchService.php, line 145

Class

SearchService
Expose synonyms of referenced entities to core Search index.

Namespace

Drupal\synonyms_search\SynonymsService\Behavior

Code

public function entityMarkForReindexMultiple(array $entity_ids, $entity_type) {
  if (empty($entity_ids)) {
    return;
  }
  $map = $this->entityFieldManager
    ->getFieldMapByFieldType('entity_reference');
  foreach ($map as $host_entity_type => $fields) {
    foreach ($fields as $field_name => $field_info) {
      $field = FieldStorageConfig::loadByName($host_entity_type, $field_name);
      if ($field && $field
        ->getSetting('target_type') == $entity_type) {
        $query = $this->entityTypeManager
          ->getStorage($host_entity_type)
          ->getQuery();
        $query
          ->condition($field_name, $entity_ids, 'IN');
        $result = $query
          ->execute();

        // For the sake of performance we do a direct query on the
        // {search_dataset} table instead of using search_mark_for_reindex()
        // function.
        // @todo Is there any smarter way to generate search type rather then
        // adding suffix of '_search'?
        if (!empty($result)) {
          $this->database
            ->update('search_dataset')
            ->fields([
            'reindex' => $this->time
              ->getRequestTime(),
          ])
            ->condition('reindex', 0)
            ->condition('type', $host_entity_type . '_search')
            ->condition('sid', array_values($result), 'IN')
            ->execute();
        }
      }
    }
  }
}