You are here

public function SelectService::getSynonymsMultiple in Synonyms 8

Extract a list of synonyms from multiple entities.

Parameters

array $entities: Array of entities from which to extract the synonyms. It should be keyed by entity ID and may only contain entities of the same type and bundle.

Return value

array Array of synonyms. The returned array will be keyed by entity ID and the inner array will have the following structure:

  • synonym: (string) Synonym itself
  • wording: (string) Formatted wording with which this synonym should be presented to the end user
1 call to SelectService::getSynonymsMultiple()
SelectService::getSynonyms in src/SynonymsService/Behavior/SelectService.php
Extract a list of synonyms from an entity.

File

src/SynonymsService/Behavior/SelectService.php, line 124

Class

SelectService
Synonyms behavior service for select widget.

Namespace

Drupal\synonyms\SynonymsService\Behavior

Code

public function getSynonymsMultiple(array $entities) {
  if (empty($entities)) {
    return [];
  }
  $synonym_configs = $this->behaviorService
    ->getSynonymConfigEntities('synonyms.behavior.select', reset($entities)
    ->getEntityTypeId(), reset($entities)
    ->bundle());
  $synonyms = [];
  foreach ($entities as $entity) {
    $synonyms[$entity
      ->id()] = [];
  }
  foreach ($synonym_configs as $synonym_config) {
    foreach ($synonym_config
      ->getProviderPluginInstance()
      ->getSynonymsMultiple($entities) as $entity_id => $entity_synonyms) {
      foreach ($entity_synonyms as $entity_synonym) {
        $synonyms[$entity_id][] = [
          'synonym' => $entity_synonym,
          'wording' => $synonym_config
            ->getProviderPluginInstance()
            ->synonymFormatWording($entity_synonym, $entities[$entity_id], $synonym_config),
        ];
      }
    }
  }
  return $synonyms;
}