You are here

public function ContentEntityTrackingManager::getIndexesForEntity in Search API 8

Retrieves all indexes that are configured to index the given entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity for which to check.

Return value

\Drupal\search_api\IndexInterface[] All indexes that are configured to index the given entity (using the default Content Entity datasource plugin).

2 calls to ContentEntityTrackingManager::getIndexesForEntity()
ContentEntityTrackingManager::entityDelete in src/Plugin/search_api/datasource/ContentEntityTrackingManager.php
Implements hook_entity_delete().
ContentEntityTrackingManager::trackEntityChange in src/Plugin/search_api/datasource/ContentEntityTrackingManager.php
Queues an entity for indexing.

File

src/Plugin/search_api/datasource/ContentEntityTrackingManager.php, line 223

Class

ContentEntityTrackingManager
Provides hook implementations on behalf of the Content Entity datasource.

Namespace

Drupal\search_api\Plugin\search_api\datasource

Code

public function getIndexesForEntity(ContentEntityInterface $entity) : array {

  // @todo This is called for every single entity insert, update or deletion
  //   on the whole site. Should maybe be cached?
  $datasource_id = 'entity:' . $entity
    ->getEntityTypeId();
  $entity_bundle = $entity
    ->bundle();
  $has_bundles = $entity
    ->getEntityType()
    ->hasKey('bundle');

  /** @var \Drupal\search_api\IndexInterface[] $indexes */
  $indexes = [];
  try {
    $indexes = $this->entityTypeManager
      ->getStorage('search_api_index')
      ->loadMultiple();
  } catch (InvalidPluginDefinitionException $e) {

    // Can't really happen, but play it safe to appease static code analysis.
  } catch (PluginNotFoundException $e) {

    // Can't really happen, but play it safe to appease static code analysis.
  }
  foreach ($indexes as $index_id => $index) {

    // Filter out indexes that don't contain the datasource in question.
    if (!$index
      ->isValidDatasource($datasource_id)) {
      unset($indexes[$index_id]);
    }
    elseif ($has_bundles) {

      // If the entity type supports bundles, we also have to filter out
      // indexes that exclude the entity's bundle.
      try {
        $config = $index
          ->getDatasource($datasource_id)
          ->getConfiguration();
      } catch (SearchApiException $e) {

        // Can't really happen, but play it safe to appease static code
        // analysis.
        unset($indexes[$index_id]);
        continue;
      }
      if (!Utility::matches($entity_bundle, $config['bundles'])) {
        unset($indexes[$index_id]);
      }
    }
  }
  return $indexes;
}