SelectService.php in Synonyms 8
File
src/SynonymsService/Behavior/SelectService.php
View source
<?php
namespace Drupal\synonyms\SynonymsService\Behavior;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\synonyms\SynonymInterface;
use Drupal\synonyms\SynonymsService\BehaviorService;
class SelectService implements SynonymsBehaviorConfigurableInterface {
use StringTranslationTrait;
protected $behaviorService;
protected $renderer;
public function __construct(BehaviorService $behavior_service, RendererInterface $renderer) {
$this->behaviorService = $behavior_service;
$this->renderer = $renderer;
}
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;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state, SynonymInterface $synonym_config) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state, SynonymInterface $synonym_config) {
return [
'wording' => $form_state
->getValue('wording'),
];
}
public function getTitle() {
return $this
->t('Select');
}
public function getSynonyms(ContentEntityInterface $entity) {
$synonyms = $this
->getSynonymsMultiple([
$entity
->id() => $entity,
]);
return $synonyms[$entity
->id()];
}
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;
}
}