View source
<?php
namespace Drupal\synonyms\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Component\Utility\Html;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\synonyms\ProviderInterface\FormatWordingTrait;
use Drupal\synonyms\BehaviorInterface\WidgetInterface;
use Drupal\synonyms\SynonymsService\BehaviorService;
class BehaviorForm extends ConfigFormBase {
use StringTranslationTrait, FormatWordingTrait;
protected $config;
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $behaviorService;
protected $entityType;
protected $bundle;
protected $renderer;
protected $statusName;
protected $wordingName;
protected $container;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, BehaviorService $behavior_service, RendererInterface $renderer, ContainerInterface $container) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->behaviorService = $behavior_service;
$this->renderer = $renderer;
$this->container = $container;
$this->entityType = $this
->getRequest()
->get('synonyms_entity_type')
->id();
$this->bundle = $this
->getRequest()
->get('bundle');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('synonyms.behavior_service'), $container
->get('renderer'), $container);
}
public function getFormId() {
return 'behavior_configuration';
}
protected function getEditableConfigNames() {
$config_names = [];
foreach ($this->behaviorService
->getBehaviorServices() as $service_id => $service) {
$config_names[] = $this
->getConfigName($service_id);
}
return $config_names;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$replacements = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#items' => [],
];
foreach ($this
->formatWordingAvailableTokens() as $token => $token_info) {
$replacements['#items'][] = Html::escape($token) . ': ' . $token_info;
}
$replacements = $this->renderer
->renderRoot($replacements);
foreach ($this->behaviorService
->getBehaviorServices() as $service_id => $service) {
$this
->setNames($service_id);
$form[$this->statusName] = [
'#type' => 'checkbox',
'#title' => $this
->t('@service service', [
'@service' => $service
->getTitle(),
]),
'#default_value' => $this
->config($this
->getConfigName($service_id))
->get('status') ?: 0,
];
if ($service instanceof WidgetInterface && $this
->showWordingForm()) {
$form[$this->wordingName] = [
'#type' => 'textfield',
'#title' => $this
->t('@widget widget wording:', [
'@widget' => $service
->getWidgetTitle(),
]),
'#default_value' => $this
->config($this
->getConfigName($service_id))
->get('wording') ?: '',
'#description' => $this
->t('Specify the wording with which @widget widget suggestions should be presented. Available replacement tokens are: @replacements If this field is left empty the @widget widget default wording will be used.', [
'@widget' => $service
->getWidgetTitle(),
'@replacements' => $replacements,
]),
'#states' => [
'visible' => [
':input[name="' . $this->statusName . '"]' => [
'checked' => TRUE,
],
],
],
];
}
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($this->behaviorService
->getBehaviorServices() as $service_id => $service) {
$this
->setNames($service_id);
$this
->config($this
->getConfigName($service_id))
->set('status', $form_state
->getValue($this->statusName))
->set('wording', $form_state
->getValue($this->wordingName))
->save();
parent::submitForm($form, $form_state);
}
}
public function entityTypeBundleBehaviorsTitle() {
if ($this->entityType == $this->bundle) {
return $this
->t('Manage behaviors of @entity_type', [
'@entity_type' => $this
->getRequest()
->get('synonyms_entity_type')
->getLabel(),
]);
}
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo($this->entityType);
return $this
->t('Manage behaviors of @entity_type @bundle', [
'@entity_type' => $this
->getRequest()
->get('synonyms_entity_type')
->getLabel(),
'@bundle' => $bundle_info[$this->bundle]['label'],
]);
}
public function setNames($service_id) {
$this->statusName = $service_id . '_status';
$this->wordingName = $service_id . '_wording';
}
public function getConfigName($service_id) {
return 'synonyms_' . $service_id . '.behavior.' . $this->entityType . '.' . $this->bundle;
}
public function showWordingForm() {
$wording_type = \Drupal::config('synonyms.settings')
->get('wording_type');
return $wording_type == 'entity' || $wording_type == 'provider';
}
}