View source
<?php
namespace Drupal\ui_patterns_settings\Plugin;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\ui_patterns_settings\Definition\PatternDefinitionSetting;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PatternSettingTypeBase extends PluginBase implements ConfigurableInterface, PatternSettingTypeInterface, ContainerFactoryPluginInterface {
protected function getSettingTypeDependencies() {
return [];
}
private $moduleHandler;
private $patternDefinition;
private $patternSettingDefinition;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
$configuration += $this
->defaultConfiguration();
$this->patternSettingDefinition = $configuration['pattern_setting_definition'];
$this->patternDefinition = $configuration['pattern_definition'];
$this->moduleHandler = $module_handler;
$this->entityTypeManager = $entity_type_manager;
unset($configuration['pattern_setting_definition']);
unset($configuration['pattern_definition']);
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
protected function getValue($value) {
if ($value === NULL) {
return $this
->getPatternSettingDefinition()
->getDefaultValue();
}
else {
return $value === NULL ? "" : $value;
}
}
protected function getPatternSettingDefinition() {
return $this->patternSettingDefinition;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = new static($configuration, $plugin_id, $plugin_definition, $container
->get('module_handler'), $container
->get('entity_type.manager'));
$translation = $container
->get('string_translation');
$plugin
->setStringTranslation($translation);
return $plugin;
}
public function alterFieldStorage(FieldStorageConfig $storage_config) {
}
public function label() {
$plugin_definition = $this
->getPluginDefinition();
return $plugin_definition['label'];
}
public function getDescription() {
$plugin_definition = $this
->getPluginDefinition();
return isset($plugin_definition['description']) ? $plugin_definition['description'] : '';
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration + $this
->defaultConfiguration();
}
public function calculateDependencies() {
return [];
}
public function preprocess($value, array $context) {
$def = $this
->getPatternSettingDefinition();
$value = $this
->settingsPreprocess($value, $context, $def);
return $value;
}
public function settingsPreprocess($value, array $context, PatternDefinitionSetting $def) {
return $value;
}
protected function tokenForm(array $form, $value, PatternDefinitionSetting $def) {
$form[$def
->getName() . "_token"] = [
'#type' => 'textfield',
'#title' => $this
->t("Token for %label", [
'%label' => $def
->getLabel(),
]),
'#default_value' => $this
->getValue($value),
'#attributes' => [
'class' => [
'js-ui-patterns-settings-show-token-link',
'js-ui-patterns-settings__token',
],
],
'#wrapper_attributes' => [
'class' => [
'js-ui-patterns-settings__token-wrapper',
],
],
];
return $form;
}
public static function validateLayout(array $element, FormStateInterface &$form_state, array &$form) {
$parents = $element['#parents'];
$value = $form_state
->getValue($parents);
$parents[count($parents) - 1] = $parents[count($parents) - 1] . '_token';
$token_value = $form_state
->getValue($parents);
if (empty($value) && empty($token_value)) {
$variant = $form_state
->getValue([
'layout_configuration',
'pattern',
'variant',
]);
if (!empty($variant)) {
$variant_def = $element['#pattern_definition']
->getVariant($variant);
$variant_ary = $variant_def
->toArray();
if (!empty($variant_ary['settings'][$element['#pattern_setting_definition']
->getName()])) {
return;
}
}
$form_state
->setError($element, t('@name field is required.', [
'@name' => $element['#title'],
]));
}
}
protected function handleInput(array &$input, PatternDefinitionSetting $def, $form_type) {
$input['#attributes']['class'][] = 'js-ui-patterns-settings__input';
$input['#wrapper_attributes']['class'][] = 'js-ui-patterns-settings__input-wrapper';
if ($def
->getRequired()) {
$input['#title'] .= ' *';
if ($form_type === 'layouts_display') {
$input['#element_validate'][] = [
PatternSettingTypeBase::class,
'validateLayout',
];
}
}
}
public function buildConfigurationForm(array $form, $value, $token_value, $form_type) {
$dependencies = $this
->getSettingTypeDependencies();
$def = $this
->getPatternSettingDefinition();
foreach ($dependencies as $dependency) {
if (!$this->moduleHandler
->moduleExists($dependency)) {
$form[$def
->getName()] = [
'#markup' => "Missing SettingType {$def->getName()} dependency {$dependency}.",
];
return $form;
}
}
$form = $this
->settingsForm($form, $value, $def, $form_type);
$form[$def
->getName()]['#pattern_setting_definition'] = $def;
$form[$def
->getName()]['#pattern_definition'] = $this->patternDefinition;
if (!empty($def
->getWeight())) {
$form[$def
->getName()]['#weight'] = $def
->getWeight();
}
if ($def
->getAllowToken()) {
$form = $this
->tokenForm($form, $token_value, $def);
if (isset($form[$def
->getName() . '_token'])) {
$classes = 'js-ui-patterns-settings__wrapper';
if (!empty($token_value)) {
$classes .= ' js-ui-patterns-settings--token-has-value';
}
$form[$def
->getName()]['#prefix'] = '<div class="' . $classes . '">';
$form[$def
->getName() . '_token']['#suffix'] = '</div>';
$form[$def
->getName() . '_token']['#pattern_setting_definition'] = $def;
$form[$def
->getName() . '_token']['#pattern_definition'] = $this->patternDefinition;
}
}
return $form;
}
public static function formGroupProcess(array &$element, FormStateInterface $form_state = NULL, &$form = []) {
if (isset($element['#pattern_setting_definition'])) {
$setting_definition = $element['#pattern_setting_definition'];
if ($setting_definition
->getGroup() !== NULL) {
$parents = $element['#parents'];
array_pop($parents);
$parents[] = $setting_definition
->getGroup();
$element['#group'] = implode('][', $parents);
}
}
return $element;
}
public function fieldStorageExposableTypes() {
return [];
}
public function preprocessExposedField(FieldItemList $items) {
foreach ($items as $item) {
return $item->value;
}
}
}