abstract class AdvancedPluginSelectorBase in Plugin 8.2
Provides a default base for most plugin selectors.
This class takes care of everything, except the actual selection element.
@internal
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorBase implements ContainerFactoryPluginInterface, PluginSelectorInterface
- class \Drupal\plugin\Plugin\Plugin\PluginSelector\AdvancedPluginSelectorBase implements ContainerFactoryPluginInterface, PluginFormInterface
- class \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorBase implements ContainerFactoryPluginInterface, PluginSelectorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of AdvancedPluginSelectorBase
3 files declare their use of AdvancedPluginSelectorBase
- AdvancedPluginSelectorBaseTest.php in tests/
src/ Unit/ Plugin/ Plugin/ PluginSelector/ AdvancedPluginSelectorBaseTest.php - RadiosTest.php in tests/
src/ Unit/ Plugin/ Plugin/ PluginSelector/ RadiosTest.php - SelectListTest.php in tests/
src/ Unit/ Plugin/ Plugin/ PluginSelector/ SelectListTest.php
File
- src/
Plugin/ Plugin/ PluginSelector/ AdvancedPluginSelectorBase.php, line 25
Namespace
Drupal\plugin\Plugin\Plugin\PluginSelectorView source
abstract class AdvancedPluginSelectorBase extends PluginSelectorBase implements ContainerFactoryPluginInterface, PluginFormInterface {
/**
* Constructs a new class instance.
*
* @param array $configuration
* @param string $plugin_id
* @param array $plugin_definition
* @param \Drupal\plugin\DefaultPluginResolver\DefaultPluginResolverInterface $default_plugin_resolver
* The default plugin resolver.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'show_selector_for_single_availability' => FALSE,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Stores a plugin selector in the form state.
*
* @param \Drupal\Core\Form\FormStateInterface
* @param \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
*
* @return string[]
* The form state storage key that contains the plugin selector.
*
* @throws \InvalidArgumentException
*/
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;
}
/**
* Gets a plugin selector from the form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param string[] $form_state_key
* The key under which the plugin selector is stored.
*
* @return \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface
*/
protected static function getPluginSelector(FormStateInterface $form_state, array $form_state_key) {
return $form_state
->get($form_state_key);
}
/**
* Implements a Form API #process callback.
*/
public static function processBuildSelectorForm(array $element, FormStateInterface $form_state, array $form) {
/** @var static $plugin_selector */
$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);
}
}
/**
* {@inheritdoc}
*/
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()) {
// Keep track of all previously selected plugins so their configuration
// does not get lost.
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 a (different) plugin was chosen and its form must be displayed,
// rebuild the form.
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);
}
}
/**
* {@inheritdoc}
*/
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);
}
}
/**
* Implements form API's #submit.
*/
public static function rebuildForm(array $form, FormStateInterface $form_state) {
$form_state
->setRebuild();
}
/**
* Implements form AJAX callback.
*/
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;
}
/**
* Builds the plugin configuration form elements.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return array
*/
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;
}
/**
* Builds the form elements for when there are no available plugins.
*/
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;
}
/**
* Builds the form elements for one plugin.
*/
public function buildOneAvailablePlugin(array $element, FormStateInterface $form_state) {
$plugin = reset($element['#available_plugins']);
// Use the only available plugin if no other was configured before, or the
// configured plugin is not available.
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;
}
/**
* Builds the form elements for multiple plugins.
*/
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;
}
/**
* Builds the form elements for the actual plugin selector.
*
* @param array $root_element
* The plugin's root element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form's state.
* @param \Drupal\Component\Plugin\PluginInspectionInterface[] $plugins
* The available plugins.
*
* @return array
* The selector's form elements.
*/
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'];
// Compute the button's name based on its position in the form, but we
// cannot use "][" to indicate nesting as we would usually do, because then
// \Drupal\Core\Form\FormBuilder::buttonWasClicked() cannot recognize the
// button when it is clicked.
$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;
}
/**
* Toggles whether or not to show the selection elements for single plugins.
*
* @param bool $show
* TRUE to show selection elements or FALSE to hide them for single plugins.
*/
public function setSelectorVisibilityForSingleAvailability($show) {
$this->configuration['show_selector_for_single_availability'] = $show;
}
/**
* Gets whether or not to show the selection elements for single plugins.
*
* @return bool
* TRUE to show selection elements or FALSE to hide them for single plugins.
*/
public function getSelectorVisibilityForSingleAvailability() {
return $this->configuration['show_selector_for_single_availability'];
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// No elements need validation.
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['show_selector_for_single_availability'] = $form_state
->getValue('show_selector_for_single_availability');
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AdvancedPluginSelectorBase:: |
public static | function | Implements form AJAX callback. | |
AdvancedPluginSelectorBase:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
AdvancedPluginSelectorBase:: |
public | function | Builds the form elements for multiple plugins. | |
AdvancedPluginSelectorBase:: |
public | function | Builds the form elements for when there are no available plugins. | |
AdvancedPluginSelectorBase:: |
public | function | Builds the form elements for one plugin. | |
AdvancedPluginSelectorBase:: |
protected | function | Builds the plugin configuration form elements. | |
AdvancedPluginSelectorBase:: |
protected | function | Builds the form elements for the actual plugin selector. | 2 |
AdvancedPluginSelectorBase:: |
public | function |
Builds the selector form. Overrides PluginSelectorBase:: |
1 |
AdvancedPluginSelectorBase:: |
public static | function |
Creates an instance of the plugin. Overrides PluginSelectorBase:: |
|
AdvancedPluginSelectorBase:: |
public | function |
Gets default configuration for this plugin. Overrides PluginSelectorBase:: |
|
AdvancedPluginSelectorBase:: |
protected static | function | Gets a plugin selector from the form state. | |
AdvancedPluginSelectorBase:: |
public | function | Gets whether or not to show the selection elements for single plugins. | |
AdvancedPluginSelectorBase:: |
public static | function | Implements a Form API #process callback. | |
AdvancedPluginSelectorBase:: |
public static | function | Implements form API's #submit. | |
AdvancedPluginSelectorBase:: |
protected static | function | Stores a plugin selector in the form state. | |
AdvancedPluginSelectorBase:: |
public | function | Toggles whether or not to show the selection elements for single plugins. | |
AdvancedPluginSelectorBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
AdvancedPluginSelectorBase:: |
public | function |
Submits the selector form. Overrides PluginSelectorInterface:: |
|
AdvancedPluginSelectorBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
AdvancedPluginSelectorBase:: |
public | function |
Validates the selector form. Overrides PluginSelectorInterface:: |
|
AdvancedPluginSelectorBase:: |
public | function |
Constructs a new class instance. Overrides PluginSelectorBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginSelectorBase:: |
protected | property | The default plugin resolver. | |
PluginSelectorBase:: |
protected | property | The previously selected plugins. | |
PluginSelectorBase:: |
protected | property | The plugin discovery of selectable plugins. | |
PluginSelectorBase:: |
protected | property | The selectable plugin factory. | |
PluginSelectorBase:: |
protected | property | The plugin type of which to select plugins. | |
PluginSelectorBase:: |
protected | property | The selected plugin. | |
PluginSelectorBase:: |
public | function | ||
PluginSelectorBase:: |
public | function |
Gets whether a plugin's configuration must be collected. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
PluginSelectorBase:: |
public | function |
Gets the human-readable description. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Gets whether previously selected plugins must be kept. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Gets the human-readable label. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Gets previously selected plugins. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Gets the selected plugin. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Returns whether a plugin must be selected. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Resets the selected plugin. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets whether a plugin's configuration must be collected. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets the human-readable description. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets whether previously selected plugins must be kept. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets the human-readable label. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets previously selected plugins. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets whether a plugin must be selected. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Overrides the plugin type's discovery. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Overrides the plugin type's factory. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets the selectable plugin type. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
public | function |
Sets the selected plugin. Overrides PluginSelectorInterface:: |
|
PluginSelectorBase:: |
protected | function | Validates the selectable plugin type. | |
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. |