You are here

public function BaseField::synonymsFind in Synonyms 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Synonyms/Provider/BaseField.php \Drupal\synonyms\Plugin\Synonyms\Provider\BaseField::synonymsFind()

Look up entities by their synonyms within a behavior implementation.

You are provided with a SQL condition that you should apply to the storage of synonyms within the provided behavior implementation. And then return result: what entities are matched by the provided condition through what synonyms.

Parameters

\Drupal\Core\Database\Query\ConditionInterface $condition: Condition that defines what to search for. Apart from normal SQL conditions as known in Drupal, it may contain the following placeholders:

For ease of work with these placeholders, you may use the FindTrait and then just invoke the $this->synonymsFindProcessCondition() method, so you won't have to worry much about it.

Return value

\Traversable Traversable result set of found synonyms and entity IDs to which those belong. Each element in the result set should be an object and should have the following structure:

  • synonym: (string) Synonym that was found and which satisfies the provided condition
  • entity_id: (int) ID of the entity to which the found synonym belongs

Overrides FindInterface::synonymsFind

File

src/Plugin/Synonyms/Provider/BaseField.php, line 92

Class

BaseField
Provide synonyms from base fields.

Namespace

Drupal\synonyms\Plugin\Synonyms\Provider

Code

public function synonymsFind(ConditionInterface $condition) {
  $entity_type_definition = $this->entityTypeManager
    ->getDefinition($this
    ->getPluginDefinition()['controlled_entity_type']);
  $entity_keys = $entity_type_definition
    ->getKeys();
  $query = $this->database
    ->select($entity_type_definition
    ->getDataTable(), 'base');
  $query
    ->addField('base', $entity_keys['id'], 'entity_id');
  $query
    ->addField('base', $this
    ->getPluginDefinition()['field'], 'synonym');
  if ($this
    ->getPluginDefinition()['controlled_entity_type'] != $this
    ->getPluginDefinition()['controlled_bundle'] && isset($entity_keys['bundle']) && $entity_keys['bundle']) {
    $query
      ->condition($entity_keys['bundle'], $this
      ->getPluginDefinition()['controlled_bundle']);
  }
  $this
    ->synonymsFindProcessCondition($condition, $this
    ->getPluginDefinition()['field'], $entity_keys['id']);
  $query
    ->condition($condition);
  return $query
    ->execute();
}