You are here

public function SelectService::selectGetSynonymsMultiple in Synonyms 2.0.x

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

File

modules/synonyms_select/src/SynonymsService/Behavior/SelectService.php, line 67

Class

SelectService
Synonyms behavior service for select widget.

Namespace

Drupal\synonyms_select\SynonymsService\Behavior

Code

public function selectGetSynonymsMultiple(array $entities) {
  if (empty($entities)) {
    return [];
  }
  $synonyms = [];
  foreach ($entities as $entity) {
    $synonyms[$entity
      ->id()] = [];
  }
  $entity_type = reset($entities)
    ->getEntityTypeId();
  $bundle = reset($entities)
    ->bundle();
  if ($this->providerService
    ->serviceIsEnabled($entity_type, $bundle, $this
    ->getId())) {
    foreach ($this->providerService
      ->getSynonymConfigEntities($entity_type, $bundle) 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, $this
              ->getId()),
          ];
        }
      }
    }
  }
  return $synonyms;
}