You are here

trait ConfigurationTrait in Synonyms 2.0.x

Provider configuration trait.

Hierarchy

1 file declares its use of ConfigurationTrait
AbstractProvider.php in src/Plugin/Synonyms/Provider/AbstractProvider.php

File

src/ProviderInterface/ConfigurationTrait.php, line 13

Namespace

Drupal\synonyms\ProviderInterface
View source
trait ConfigurationTrait {
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'wording' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration;
  }

  /**
   * {@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 = \Drupal::service('renderer')
      ->renderRoot($replacements);
    $wording = isset($configuration['wording']) ? $configuration['wording'] : '';
    $form['wording'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Wording for this provider'),
      '#default_value' => $wording,
      '#description' => $this
        ->t('Specify the wording with which this entry should be presented. Available replacement tokens are: @replacements Note: To avoid unnecessary complexity there is no per-widget wording configuration here at provider level. So, this wording will be used by all installed synonyms-friendly widgets.', [
        '@replacements' => $replacements,
      ]),
    ];
    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'),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurationTrait::buildConfigurationForm public function
ConfigurationTrait::defaultConfiguration public function
ConfigurationTrait::getConfiguration public function
ConfigurationTrait::setConfiguration public function
ConfigurationTrait::submitConfigurationForm public function
ConfigurationTrait::validateConfigurationForm public function
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.