You are here

public function ReverseEntityReferences::getPropertyDefinitions in Search API 8

Retrieves the properties this processor defines for the given datasource.

Property names have to start with a letter or an underscore, followed by any number of letters, numbers and underscores. To avoid collisions, it is also recommended to prefix the property name with the identifier of the module defining the processor.

Parameters

\Drupal\search_api\Datasource\DatasourceInterface|null $datasource: (optional) The datasource this set of properties belongs to. If NULL, the datasource-independent properties should be added (or modified).

Return value

\Drupal\search_api\Processor\ProcessorPropertyInterface[] An array of property definitions for that datasource, keyed by property names.

Overrides ProcessorPluginBase::getPropertyDefinitions

File

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

Class

ReverseEntityReferences
Allows indexing of reverse entity references.

Namespace

Drupal\search_api\Plugin\search_api\processor

Code

public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
  $properties = [];
  if (!$datasource || !$datasource
    ->getEntityTypeId()) {
    return $properties;
  }
  $references = $this
    ->getEntityReferences();
  $entity_type_id = $datasource
    ->getEntityTypeId();
  if (isset($references[$entity_type_id])) {
    foreach ($references[$entity_type_id] as $key => $reference) {
      $entity_type_id = $reference['entity_type'];
      try {
        $entity_type = $this
          ->getEntityTypeManager()
          ->getDefinition($entity_type_id);
      } catch (PluginNotFoundException $e) {
        continue;
      }
      $args = [
        '%entity_type' => $entity_type
          ->getLabel(),
        '%property' => $reference['label'],
      ];
      $definition = [
        'label' => $this
          ->t('Reverse reference: %entity_type using %property', $args),
        'description' => $this
          ->t("All %entity_type entities that reference this item via the %property field."),
        'type' => "entity:{$entity_type_id}",
        'processor_id' => $this
          ->getPluginId(),
        // We can't really know whether this will end up being multi-valued, so
        // we err on the side of caution.
        'is_list' => TRUE,
      ];
      $property = new EntityProcessorProperty($definition);
      $property
        ->setEntityTypeId($entity_type_id);
      $properties["search_api_reverse_entity_references_{$key}"] = $property;
    }
  }
  return $properties;
}