You are here

class SelectService in Synonyms 2.0.x

Synonyms behavior service for select widget.

Hierarchy

Expanded class hierarchy of SelectService

1 string reference to 'SelectService'
synonyms_select.services.yml in modules/synonyms_select/synonyms_select.services.yml
modules/synonyms_select/synonyms_select.services.yml
1 service uses SelectService
synonyms.behavior.select in modules/synonyms_select/synonyms_select.services.yml
Drupal\synonyms_select\SynonymsService\Behavior\SelectService

File

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

Namespace

Drupal\synonyms_select\SynonymsService\Behavior
View source
class SelectService implements BehaviorInterface, WidgetInterface {
  use StringTranslationTrait;

  /**
   * The synonyms provider service.
   *
   * @var \Drupal\synonyms\SynonymsService\ProviderService
   */
  protected $providerService;

  /**
   * SelectService constructor.
   */
  public function __construct(ProviderService $provider_service) {
    $this->providerService = $provider_service;
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return 'select';
  }

  /**
   * {@inheritdoc}
   */
  public function getTitle() {
    return $this
      ->t('Select');
  }

  /**
   * {@inheritdoc}
   */
  public function getWidgetTitle() {
    return $this
      ->t('Synonyms-friendly select');
  }

  /**
   * Extract a list of synonyms from multiple entities.
   *
   * @param 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 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
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SelectService::$providerService protected property The synonyms provider service.
SelectService::getId public function Get machine readable ID of this behavior. Overrides BehaviorInterface::getId
SelectService::getTitle public function Get human readable title of this behavior. Overrides BehaviorInterface::getTitle
SelectService::getWidgetTitle public function Get human readable title of this widget. Overrides WidgetInterface::getWidgetTitle
SelectService::selectGetSynonymsMultiple public function Extract a list of synonyms from multiple entities.
SelectService::__construct public function SelectService constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.