You are here

class SelectService in Synonyms 8

Synonyms behavior service for select widget.

Hierarchy

Expanded class hierarchy of SelectService

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

File

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

Namespace

Drupal\synonyms\SynonymsService\Behavior
View source
class SelectService implements SynonymsBehaviorConfigurableInterface {
  use StringTranslationTrait;

  /**
   * The synonyms behavior service.
   *
   * @var \Drupal\synonyms\SynonymsService\BehaviorService
   */
  protected $behaviorService;

  /**
   * The renderer interface.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * SelectService constructor.
   */
  public function __construct(BehaviorService $behavior_service, RendererInterface $renderer) {
    $this->behaviorService = $behavior_service;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state, array $configuration, SynonymInterface $synonym_config) {
    $replacements = [
      '#theme' => 'item_list',
      '#list_type' => 'ul',
      '#items' => [],
    ];
    foreach ($synonym_config
      ->getProviderPluginInstance()
      ->formatWordingAvailableTokens() as $token => $token_info) {
      $replacements['#items'][] = Html::escape($token) . ': ' . $token_info;
    }
    $replacements = $this->renderer
      ->renderRoot($replacements);
    $wording = isset($configuration['wording']) ? $configuration['wording'] : '';
    $form['wording'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Wording for select entry'),
      '#default_value' => $wording,
      '#description' => $this
        ->t('Specify the wording with which the select entry should be presented. Available replacement tokens are: @replacements', [
        '@replacements' => $replacements,
      ]),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state, SynonymInterface $synonym_config) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state, SynonymInterface $synonym_config) {
    return [
      'wording' => $form_state
        ->getValue('wording'),
    ];
  }

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

  /**
   * Extract a list of synonyms from an entity.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
   *   Entity from which to extract the synonyms.
   *
   * @return array
   *   Array of synonyms. Each sub 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 getSynonyms(ContentEntityInterface $entity) {
    $synonyms = $this
      ->getSynonymsMultiple([
      $entity
        ->id() => $entity,
    ]);
    return $synonyms[$entity
      ->id()];
  }

  /**
   * 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 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SelectService::$behaviorService protected property The synonyms behavior service.
SelectService::$renderer protected property The renderer interface.
SelectService::buildConfigurationForm public function Build configuration form. Overrides SynonymsBehaviorConfigurableInterface::buildConfigurationForm
SelectService::getSynonyms public function Extract a list of synonyms from an entity.
SelectService::getSynonymsMultiple public function Extract a list of synonyms from multiple entities.
SelectService::getTitle public function Get human readable title of this behavior. Overrides SynonymsBehaviorInterface::getTitle
SelectService::submitConfigurationForm public function Process submitted values and generate new configuration. Overrides SynonymsBehaviorConfigurableInterface::submitConfigurationForm
SelectService::validateConfigurationForm public function Validate submitted values into your configuration form. Overrides SynonymsBehaviorConfigurableInterface::validateConfigurationForm
SelectService::__construct public function SelectService constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.