SelectList.php in Plugin 8.2
File
src/Plugin/Plugin/PluginSelector/SelectList.php
View source
<?php
namespace Drupal\plugin\Plugin\Plugin\PluginSelector;
use Drupal\Core\Form\FormStateInterface;
use Drupal\plugin\PluginDefinition\PluginLabelDefinitionInterface;
use Drupal\plugin\PluginHierarchyTrait;
class SelectList extends AdvancedPluginSelectorBase {
use PluginHierarchyTrait;
protected function buildSelector(array $root_element, FormStateInterface $form_state, array $plugins) {
$element = parent::buildSelector($root_element, $form_state, $plugins);
$element['container']['plugin_id'] = [
'#ajax' => [
'callback' => [
get_class(),
'ajaxRebuildForm',
],
'effect' => 'fade',
'event' => 'change',
'trigger_as' => [
'name' => $element['container']['change']['#name'],
],
],
'#default_value' => $this
->getSelectedPlugin() ? $this
->getSelectedPlugin()
->getPluginId() : NULL,
'#empty_value' => '',
'#options' => $this
->buildOptionsLevel($this
->buildPluginHierarchy($this->selectablePluginDiscovery)),
'#required' => $this
->isRequired(),
'#title' => $this
->getLabel(),
'#description' => $this
->getDescription(),
'#type' => 'select',
];
return $element;
}
protected function buildOptionsLevel(array $hierarchy, $depth = 0) {
$plugin_definitions = $this->selectablePluginDiscovery
->getDefinitions();
$options = [];
$prefix = $depth ? str_repeat('-', $depth) . ' ' : '';
foreach ($hierarchy as $plugin_id => $child_plugin_ids) {
$plugin_definition = $plugin_definitions[$plugin_id];
$label = $plugin_definition instanceof PluginLabelDefinitionInterface ? $plugin_definition
->getLabel() : $plugin_definition
->getId();
$options[$plugin_id] = $prefix . $label;
$options += $this
->buildOptionsLevel($child_plugin_ids, $depth + 1);
}
return $options;
}
}
Classes
Name |
Description |
SelectList |
Provides a plugin selector using a <select> element. |