View source
<?php
namespace Drupal\plugin\Plugin\Plugin\PluginSelector;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\plugin\DefaultPluginResolver\DefaultPluginResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class AdvancedPluginSelectorBase extends PluginSelectorBase implements ContainerFactoryPluginInterface, PluginFormInterface {
public function __construct(array $configuration, $plugin_id, array $plugin_definition, DefaultPluginResolverInterface $default_plugin_resolver, TranslationInterface $string_translation) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $default_plugin_resolver);
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.default_plugin_resolver'), $container
->get('string_translation'));
}
public function defaultConfiguration() {
return [
'show_selector_for_single_availability' => FALSE,
] + parent::defaultConfiguration();
}
public function buildSelectorForm(array $form, FormStateInterface $form_state) {
$form = parent::buildSelectorForm($form, $form_state);
$available_plugins = [];
$cacheability_metadata = CacheableMetadata::createFromRenderArray($form);
foreach (array_keys($this->selectablePluginDiscovery
->getDefinitions()) as $plugin_id) {
$available_plugin = $this->selectablePluginFactory
->createInstance($plugin_id);
if ($available_plugin instanceof PluginInspectionInterface) {
$available_plugins[] = $available_plugin;
$cacheability_metadata = $cacheability_metadata
->merge(CacheableMetadata::createFromObject($available_plugin));
}
}
$cacheability_metadata
->applyTo($form);
$plugin_selector_form_state_key = static::setPluginSelector($form_state, $this);
$form['container'] = [
'#attributes' => [
'class' => [
'plugin-selector-' . Html::getClass($this
->getPluginId()),
],
],
'#available_plugins' => $available_plugins,
'#plugin_selector_form_state_key' => $plugin_selector_form_state_key,
'#process' => [
[
get_class(),
'processBuildSelectorForm',
],
],
'#tree' => TRUE,
'#type' => 'container',
];
return $form;
}
protected static function setPluginSelector(FormStateInterface $form_state, PluginSelectorInterface $plugin_selector) {
do {
$key = [
get_class(),
mt_rand(),
];
} while ($form_state
->has($key));
$form_state
->set($key, $plugin_selector);
return $key;
}
protected static function getPluginSelector(FormStateInterface $form_state, array $form_state_key) {
return $form_state
->get($form_state_key);
}
public static function processBuildSelectorForm(array $element, FormStateInterface $form_state, array $form) {
$plugin_selector = static::getPluginSelector($form_state, $element['#plugin_selector_form_state_key']);
if (count($element['#available_plugins']) == 0) {
return $plugin_selector
->buildNoAvailablePlugins($element, $form_state);
}
elseif (count($element['#available_plugins']) == 1 && !$plugin_selector
->getSelectorVisibilityForSingleAvailability()) {
return $plugin_selector
->buildOneAvailablePlugin($element, $form_state);
}
else {
return $plugin_selector
->buildMultipleAvailablePlugins($element, $form_state);
}
}
public function validateSelectorForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$plugin_id = NestedArray::getValue($values, array_merge($form['container']['#parents'], array(
'select',
'container',
'plugin_id',
)));
$selected_plugin = $this
->getSelectedPlugin();
if (!$selected_plugin && $plugin_id || $selected_plugin && $plugin_id != $selected_plugin
->getPluginId()) {
if (isset($this
->getPreviouslySelectedPlugins()[$plugin_id])) {
$this
->setSelectedPlugin($this
->getPreviouslySelectedPlugins()[$plugin_id]);
}
elseif ($plugin_id) {
$this
->setSelectedPlugin($this->selectablePluginFactory
->createInstance($plugin_id));
}
else {
$this
->resetSelectedPlugin();
}
if ($this
->getCollectPluginConfiguration() && $this
->getSelectedPlugin() instanceof PluginFormInterface) {
$form_state
->setRebuild();
}
}
elseif ($this
->getCollectPluginConfiguration() && $selected_plugin instanceof PluginFormInterface) {
$selected_plugin
->validateConfigurationForm($form['container']['plugin_form'], $form_state);
}
}
public function submitSelectorForm(array &$form, FormStateInterface $form_state) {
$selectedPlugin = $this
->getSelectedPlugin();
if ($this
->getCollectPluginConfiguration() && $selectedPlugin instanceof PluginFormInterface) {
$selectedPlugin
->submitConfigurationForm($form['container']['plugin_form'], $form_state);
}
}
public static function rebuildForm(array $form, FormStateInterface $form_state) {
$form_state
->setRebuild();
}
public static function ajaxRebuildForm(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
$form_parents = array_slice($triggering_element['#array_parents'], 0, -3);
$root_element = NestedArray::getValue($form, $form_parents);
$response = new AjaxResponse();
$response
->addCommand(new ReplaceCommand(sprintf('[data-drupal-selector="%s"]', $root_element['plugin_form']['#attributes']['data-drupal-selector']), $root_element['plugin_form']));
return $response;
}
protected function buildPluginForm(FormStateInterface $form_state) {
$element = [
'#attributes' => [
'class' => [
Html::getClass(sprintf('plugin-selector-%s-plugin-form', $this
->getPluginId())),
],
],
'#type' => 'container',
];
$selectedPlugin = $this
->getSelectedPlugin();
if ($this
->getCollectPluginConfiguration() && $selectedPlugin instanceof PluginFormInterface) {
$element += $selectedPlugin
->buildConfigurationForm([], $form_state);
}
return $element;
}
public function buildNoAvailablePlugins(array $element, FormStateInterface $form_state) {
$element['select']['container'] = [
'#type' => 'container',
];
$element['select']['container']['plugin_id'] = [
'#type' => 'value',
'#value' => NULL,
];
$element['select']['message'] = [
'#markup' => $this
->t('There are no available options.'),
'#title' => $this
->getLabel(),
'#type' => 'item',
];
return $element;
}
public function buildOneAvailablePlugin(array $element, FormStateInterface $form_state) {
$plugin = reset($element['#available_plugins']);
if (is_null($this
->getSelectedPlugin()) || $this
->getSelectedPlugin()
->getPluginId() != $plugin
->getPluginId()) {
$this
->setSelectedPlugin($plugin);
}
$element['select']['message'] = [
'#title' => $this
->getLabel(),
'#type' => 'item',
];
$element['select']['container'] = [
'#type' => 'container',
];
$element['select']['container']['plugin_id'] = [
'#type' => 'value',
'#value' => $this
->getSelectedPlugin()
->getPluginId(),
];
$element['plugin_form'] = $this
->buildPluginForm($form_state);
return $element;
}
public function buildMultipleAvailablePlugins(array $element, FormStateInterface $form_state) {
$plugins = $element['#available_plugins'];
$element['select'] = $this
->buildSelector($element, $form_state, $plugins);
$element['plugin_form'] = $this
->buildPluginForm($form_state);
return $element;
}
protected function buildSelector(array $root_element, FormStateInterface $form_state, array $plugins) {
$build['container'] = [
'#attributes' => [
'class' => [
'plugin-selector-' . Html::getClass($this
->getPluginId() . '-selector'),
],
],
'#type' => 'container',
];
$build['container']['plugin_id'] = [
'#markup' => 'This element must be overridden to provide the plugin ID.',
];
$root_element_parents = $root_element['#parents'];
$change_button_name_parts = array_merge($root_element_parents, [
'select',
'container',
'change',
]);
$change_button_name = implode('__', $change_button_name_parts);
$build['container']['change'] = [
'#ajax' => [
'callback' => [
get_class(),
'ajaxRebuildForm',
],
],
'#attributes' => [
'class' => [
'js-hide',
],
],
'#limit_validation_errors' => [
array_merge($root_element['#parents'], [
'select',
'plugin_id',
]),
],
'#name' => $change_button_name,
'#submit' => array(
array(
get_class(),
'rebuildForm',
),
),
'#type' => 'submit',
'#value' => $this
->t('Choose'),
];
return $build;
}
public function setSelectorVisibilityForSingleAvailability($show) {
$this->configuration['show_selector_for_single_availability'] = $show;
}
public function getSelectorVisibilityForSingleAvailability() {
return $this->configuration['show_selector_for_single_availability'];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = [];
$form['show_selector_for_single_availability'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide selector if only a single plugin is available'),
'#default_value' => $this->configuration['show_selector_for_single_availability'],
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['show_selector_for_single_availability'] = $form_state
->getValue('show_selector_for_single_availability');
}
}