You are here

public function ReverseEntityReferences::getEntityReferences in Search API 8

Collects all entity references.

Return value

array[][] An associative array of entity reference information keyed by the referenced entity type's ID and a custom identifier for the property (consisting of referencing entity type and property name), with values being associative arrays with the following keys:

  • label: The property label.
  • entity_type: The referencing entity type.
  • property: The property name.
2 calls to ReverseEntityReferences::getEntityReferences()
ReverseEntityReferences::addFieldValues in src/Plugin/search_api/processor/ReverseEntityReferences.php
Adds the values of properties defined by this processor to the item.
ReverseEntityReferences::getPropertyDefinitions in src/Plugin/search_api/processor/ReverseEntityReferences.php
Retrieves the properties this processor defines for the given datasource.

File

src/Plugin/search_api/processor/ReverseEntityReferences.php, line 360

Class

ReverseEntityReferences
Allows indexing of reverse entity references.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function getEntityReferences() {
  if ($this->references !== NULL) {
    return $this->references;
  }

  // Property labels differ by language, so we need to vary the cache
  // according to the current language.
  $langcode = $this
    ->getLanguageManager()
    ->getCurrentLanguage()
    ->getId();
  $cid = "search_api:reverse_entity_references:{$langcode}";
  $cache = $this
    ->getCache()
    ->get($cid);
  if (isset($cache->data)) {
    $this->references = $cache->data;
  }
  else {
    $this->references = [];
    $entity_types = $this
      ->getEntityTypeManager()
      ->getDefinitions();
    $field_manager = $this
      ->getEntityFieldManager();
    $entity_type_bundle_info = $this
      ->getEntityTypeBundleInfo();
    foreach ($entity_types as $entity_type_id => $entity_type) {
      if (!$entity_type instanceof ContentEntityTypeInterface) {
        continue;
      }

      /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $properties */
      $properties = $field_manager
        ->getBaseFieldDefinitions($entity_type_id);
      $bundles = $entity_type_bundle_info
        ->getBundleInfo($entity_type_id);
      foreach ($bundles as $bundle => $info) {
        $properties += $field_manager
          ->getFieldDefinitions($entity_type_id, $bundle);
      }
      foreach ($properties as $name => $property) {
        if ($property
          ->getType() !== 'entity_reference') {
          continue;
        }
        $settings = $property
          ->getSettings();
        if (empty($settings['target_type'])) {
          continue;
        }
        $this->references[$settings['target_type']]["{$entity_type_id}__{$name}"] = [
          'label' => $property
            ->getLabel(),
          'entity_type' => $entity_type_id,
          'property' => $name,
        ];
      }
    }
    $tags = [
      'entity_types',
      'entity_bundles',
      'entity_field_info',
    ];
    $this
      ->getCache()
      ->set($cid, $this->references, Cache::PERMANENT, $tags);
  }
  return $this->references;
}