PluginSelectorBase.php in Plugin 8.2
File
src/Plugin/Plugin/PluginSelector/PluginSelectorBase.php
View source
<?php
namespace Drupal\plugin\Plugin\Plugin\PluginSelector;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Factory\FactoryInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\plugin\DefaultPluginResolver\DefaultPluginResolverInterface;
use Drupal\plugin\PluginDiscovery\TypedDefinitionEnsuringPluginDiscoveryDecorator;
use Drupal\plugin\PluginType\PluginTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PluginSelectorBase extends PluginBase implements PluginSelectorInterface, ContainerFactoryPluginInterface {
protected $defaultPluginResolver;
protected $previouslySelectedPlugins = [];
protected $selectablePluginDiscovery;
protected $selectablePluginFactory;
protected $selectablePluginType;
protected $selectedPlugin;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DefaultPluginResolverInterface $default_plugin_resolver) {
$configuration += $this
->defaultConfiguration();
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->defaultPluginResolver = $default_plugin_resolver;
}
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'));
}
public function calculateDependencies() {
return [];
}
public function defaultConfiguration() {
return [
'description' => NULL,
'label' => NULL,
'required' => FALSE,
'collect_plugin_configuration' => TRUE,
'keep_previously_selected_plugins' => TRUE,
];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
return $this;
}
public function setLabel($label) {
$this->configuration['label'] = $label;
return $this;
}
public function getLabel() {
return $this->configuration['label'];
}
public function setDescription($description) {
$this->configuration['description'] = $description;
return $this;
}
public function getDescription() {
return $this->configuration['description'];
}
public function setRequired($required = TRUE) {
$this->configuration['required'] = $required;
return $this;
}
public function isRequired() {
return $this->configuration['required'];
}
public function setCollectPluginConfiguration($collect = TRUE) {
$this->configuration['collect_plugin_configuration'] = $collect;
return $this;
}
public function getCollectPluginConfiguration() {
return $this->configuration['collect_plugin_configuration'];
}
public function setKeepPreviouslySelectedPlugins($keep = TRUE) {
$this->configuration['keep_previously_selected_plugins'] = $keep;
if ($keep === FALSE) {
$this
->setPreviouslySelectedPlugins([]);
}
return $this;
}
public function getKeepPreviouslySelectedPlugins() {
return $this->configuration['keep_previously_selected_plugins'];
}
public function setPreviouslySelectedPlugins(array $plugins) {
$this->previouslySelectedPlugins = $plugins;
return $this;
}
public function getPreviouslySelectedPlugins() {
return $this->previouslySelectedPlugins;
}
public function getSelectedPlugin() {
return $this->selectedPlugin;
}
public function setSelectedPlugin(PluginInspectionInterface $plugin) {
$this
->validateSelectablePluginType();
$this->selectedPlugin = $plugin;
if ($this
->getKeepPreviouslySelectedPlugins()) {
$this->previouslySelectedPlugins[$plugin
->getPluginId()] = $plugin;
}
return $this;
}
public function resetSelectedPlugin() {
$this->selectedPlugin = NULL;
return $this;
}
public function setSelectablePluginType(PluginTypeInterface $plugin_type) {
$this->selectablePluginDiscovery = new TypedDefinitionEnsuringPluginDiscoveryDecorator($plugin_type);
$this->selectablePluginFactory = $plugin_type
->getPluginManager();
$this->selectablePluginType = $plugin_type;
$default_plugin = $this->defaultPluginResolver
->createDefaultPluginInstance($plugin_type);
if ($default_plugin) {
$this
->setSelectedPlugin($default_plugin);
}
return $this;
}
public function setSelectablePluginDiscovery(DiscoveryInterface $plugin_discovery) {
$this
->validateSelectablePluginType();
$this->selectablePluginDiscovery = new TypedDefinitionEnsuringPluginDiscoveryDecorator($this->selectablePluginType, $plugin_discovery);
return $this;
}
public function setSelectablePluginFactory(FactoryInterface $plugin_factory) {
$this
->validateSelectablePluginType();
$this->selectablePluginFactory = $plugin_factory;
return $this;
}
public function buildSelectorForm(array $form, FormStateInterface $form_state) {
$this
->validateSelectablePluginType();
return [];
}
protected function validateSelectablePluginType() {
if (!$this->selectablePluginType) {
throw new \RuntimeException('A plugin type must be set through static::setSelectablePluginType() first.');
}
}
}