class SelectService in Synonyms 8
Synonyms behavior service for select widget.
Hierarchy
- class \Drupal\synonyms\SynonymsService\Behavior\SelectService implements SynonymsBehaviorConfigurableInterface uses StringTranslationTrait
Expanded class hierarchy of SelectService
1 string reference to 'SelectService'
1 service uses SelectService
File
- src/
SynonymsService/ Behavior/ SelectService.php, line 16
Namespace
Drupal\synonyms\SynonymsService\BehaviorView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SelectService:: |
protected | property | The synonyms behavior service. | |
SelectService:: |
protected | property | The renderer interface. | |
SelectService:: |
public | function |
Build configuration form. Overrides SynonymsBehaviorConfigurableInterface:: |
|
SelectService:: |
public | function | Extract a list of synonyms from an entity. | |
SelectService:: |
public | function | Extract a list of synonyms from multiple entities. | |
SelectService:: |
public | function |
Get human readable title of this behavior. Overrides SynonymsBehaviorInterface:: |
|
SelectService:: |
public | function |
Process submitted values and generate new configuration. Overrides SynonymsBehaviorConfigurableInterface:: |
|
SelectService:: |
public | function |
Validate submitted values into your configuration form. Overrides SynonymsBehaviorConfigurableInterface:: |
|
SelectService:: |
public | function | SelectService constructor. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |