You are here

public function FindSynonyms::findSynonyms in Synonyms 8

Lookup entity IDs by the $condition.

Parameters

\Drupal\Core\Database\Query\Condition $condition: Condition which defines what to search for.

\Drupal\Core\Entity\EntityTypeInterface $entity_type: Entity type within which to search.

string|array $bundle: Either single bundle string or array of such within which to search. NULL stands for no filtering by bundle, i.e. searching among all bundles.

string|array $service_id: Either a single behavior service ID or an array of them within which to execute the lookup. You may also use the wildcard * to search among all supported behaviors.

Return value

array Array of looked up synonyms/entities. Each element in this array will be an object with the following structure:

  • synonym: (string) synonym that was looked up
  • entity_id: (int) ID of the entity which this synonym belongs to

File

src/SynonymsService/FindSynonyms.php, line 57

Class

FindSynonyms
Service that allows to look up entities by their synonyms.

Namespace

Drupal\synonyms\SynonymsService

Code

public function findSynonyms(Condition $condition, EntityTypeInterface $entity_type, $bundle = NULL, $service_id = 'synonyms.behavior.autocomplete') {
  if (!$entity_type
    ->getKey('bundle')) {
    $bundle = $entity_type
      ->id();
  }
  $lookup = [];
  if ($service_id == '*') {
    $service_id = array_keys($this->behaviorService
      ->getBehaviorServices());
  }
  if (!is_array($service_id)) {
    $service_id = [
      $service_id,
    ];
  }
  if (is_null($bundle)) {
    $bundle = array_keys($this->entityTypeBundleInfo
      ->getBundleInfo($entity_type
      ->id()));
  }
  foreach ($service_id as $service_id_value) {
    foreach ($this->behaviorService
      ->getSynonymConfigEntities($service_id_value, $entity_type
      ->id(), $bundle) as $synonym_config) {
      foreach ($synonym_config
        ->getProviderPluginInstance()
        ->synonymsFind(clone $condition) as $synonym) {
        $lookup[] = $synonym;
      }
    }
  }
  return $lookup;
}