Ejemplo n.º 1
0
 /**
  * Retrieves all indexes that are configured to index the given entity.
  *
  * @param \Drupal\Core\Entity\ContentEntityInterface $entity
  *   The entity for which to check.
  *
  * @return \Drupal\search_api\IndexInterface[]
  *   All indexes that are configured to index the given entity (using this
  *   datasource class).
  */
 public static function getIndexesForEntity(ContentEntityInterface $entity)
 {
     $entity_type = $entity->getEntityTypeId();
     $datasource_id = 'entity:' . $entity_type;
     $entity_bundle = $entity->bundle();
     $index_names = \Drupal::entityQuery('search_api_index')->condition('datasources.*', $datasource_id)->execute();
     if (!$index_names) {
         return array();
     }
     // Needed for PhpStorm. See https://youtrack.jetbrains.com/issue/WI-23395.
     /** @var \Drupal\search_api\IndexInterface[] $indexes */
     $indexes = Index::loadMultiple($index_names);
     // If the datasource's entity type supports bundles, we have to filter the
     // indexes for whether they also include the specific bundle of the given
     // entity. Otherwise, we are done.
     if ($entity_type !== $entity_bundle) {
         foreach ($indexes as $index_id => $index) {
             try {
                 $config = $index->getDatasource($datasource_id)->getConfiguration();
                 $default = !empty($config['default']);
                 $bundle_set = !empty($config['bundles'][$entity_bundle]);
                 if ($default == $bundle_set) {
                     unset($indexes[$index_id]);
                 }
             } catch (SearchApiException $e) {
                 unset($indexes[$index_id]);
             }
         }
     }
     return $indexes;
 }