You are here

abstract class PluginSelectorBase in Plugin 8.2

Provides a base plugin selector.

Plugins extending this class should provide a configuration schema that extends plugin_selector.plugin_configuration.plugin_selector.plugin_selector_base.

Hierarchy

Expanded class hierarchy of PluginSelectorBase

1 file declares its use of PluginSelectorBase
PluginSelectorBaseTest.php in tests/src/Unit/Plugin/Plugin/PluginSelector/PluginSelectorBaseTest.php

File

src/Plugin/Plugin/PluginSelector/PluginSelectorBase.php, line 23

Namespace

Drupal\plugin\Plugin\Plugin\PluginSelector
View source
abstract class PluginSelectorBase extends PluginBase implements PluginSelectorInterface, ContainerFactoryPluginInterface {

  /**
   * The default plugin resolver.
   *
   * @var \Drupal\plugin\DefaultPluginResolver\DefaultPluginResolverInterface
   */
  protected $defaultPluginResolver;

  /**
   * The previously selected plugins.
   *
   * @var \Drupal\Component\Plugin\PluginInspectionInterface[]
   */
  protected $previouslySelectedPlugins = [];

  /**
   * The plugin discovery of selectable plugins.
   *
   * @var \Drupal\plugin\PluginDiscovery\TypedDiscoveryInterface
   */
  protected $selectablePluginDiscovery;

  /**
   * The selectable plugin factory.
   *
   * @var \Drupal\Component\Plugin\Factory\FactoryInterface
   */
  protected $selectablePluginFactory;

  /**
   * The plugin type of which to select plugins.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeInterface
   */
  protected $selectablePluginType;

  /**
   * The selected plugin.
   *
   * @var \Drupal\Component\Plugin\PluginInspectionInterface
   */
  protected $selectedPlugin;

  /**
   * Constructs a new instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\plugin\DefaultPluginResolver\DefaultPluginResolverInterface $default_plugin_resolver
   *   The default plugin resolver.
   */
  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;
  }

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'description' => NULL,
      'label' => NULL,
      'required' => FALSE,
      'collect_plugin_configuration' => TRUE,
      'keep_previously_selected_plugins' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLabel($label) {
    $this->configuration['label'] = $label;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->configuration['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function setDescription($description) {
    $this->configuration['description'] = $description;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->configuration['description'];
  }

  /**
   * {@inheritdoc}
   */
  public function setRequired($required = TRUE) {
    $this->configuration['required'] = $required;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isRequired() {
    return $this->configuration['required'];
  }

  /**
   * {@inheritdoc}
   */
  public function setCollectPluginConfiguration($collect = TRUE) {
    $this->configuration['collect_plugin_configuration'] = $collect;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCollectPluginConfiguration() {
    return $this->configuration['collect_plugin_configuration'];
  }

  /**
   * {@inheritdoc}
   */
  public function setKeepPreviouslySelectedPlugins($keep = TRUE) {
    $this->configuration['keep_previously_selected_plugins'] = $keep;
    if ($keep === FALSE) {
      $this
        ->setPreviouslySelectedPlugins([]);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getKeepPreviouslySelectedPlugins() {
    return $this->configuration['keep_previously_selected_plugins'];
  }

  /**
   * {@inheritdoc}
   */
  public function setPreviouslySelectedPlugins(array $plugins) {
    $this->previouslySelectedPlugins = $plugins;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getPreviouslySelectedPlugins() {
    return $this->previouslySelectedPlugins;
  }

  /**
   * {@inheritdoc}
   */
  public function getSelectedPlugin() {
    return $this->selectedPlugin;
  }

  /**
   * {@inheritdoc}
   */
  public function setSelectedPlugin(PluginInspectionInterface $plugin) {
    $this
      ->validateSelectablePluginType();
    $this->selectedPlugin = $plugin;
    if ($this
      ->getKeepPreviouslySelectedPlugins()) {
      $this->previouslySelectedPlugins[$plugin
        ->getPluginId()] = $plugin;
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function resetSelectedPlugin() {
    $this->selectedPlugin = NULL;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function setSelectablePluginDiscovery(DiscoveryInterface $plugin_discovery) {
    $this
      ->validateSelectablePluginType();
    $this->selectablePluginDiscovery = new TypedDefinitionEnsuringPluginDiscoveryDecorator($this->selectablePluginType, $plugin_discovery);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setSelectablePluginFactory(FactoryInterface $plugin_factory) {
    $this
      ->validateSelectablePluginType();
    $this->selectablePluginFactory = $plugin_factory;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function buildSelectorForm(array $form, FormStateInterface $form_state) {
    $this
      ->validateSelectablePluginType();
    return [];
  }

  /**
   * Validates the selectable plugin type.
   *
   * @throw \RuntimeException
   */
  protected function validateSelectablePluginType() {
    if (!$this->selectablePluginType) {
      throw new \RuntimeException('A plugin type must be set through static::setSelectablePluginType() first.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSelectorBase::$defaultPluginResolver protected property The default plugin resolver.
PluginSelectorBase::$previouslySelectedPlugins protected property The previously selected plugins.
PluginSelectorBase::$selectablePluginDiscovery protected property The plugin discovery of selectable plugins.
PluginSelectorBase::$selectablePluginFactory protected property The selectable plugin factory.
PluginSelectorBase::$selectablePluginType protected property The plugin type of which to select plugins.
PluginSelectorBase::$selectedPlugin protected property The selected plugin.
PluginSelectorBase::buildSelectorForm public function Builds the selector form. Overrides PluginSelectorInterface::buildSelectorForm 1
PluginSelectorBase::calculateDependencies public function
PluginSelectorBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
PluginSelectorBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
PluginSelectorBase::getCollectPluginConfiguration public function Gets whether a plugin's configuration must be collected. Overrides PluginSelectorInterface::getCollectPluginConfiguration
PluginSelectorBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
PluginSelectorBase::getDescription public function Gets the human-readable description. Overrides PluginSelectorInterface::getDescription
PluginSelectorBase::getKeepPreviouslySelectedPlugins public function Gets whether previously selected plugins must be kept. Overrides PluginSelectorInterface::getKeepPreviouslySelectedPlugins
PluginSelectorBase::getLabel public function Gets the human-readable label. Overrides PluginSelectorInterface::getLabel
PluginSelectorBase::getPreviouslySelectedPlugins public function Gets previously selected plugins. Overrides PluginSelectorInterface::getPreviouslySelectedPlugins
PluginSelectorBase::getSelectedPlugin public function Gets the selected plugin. Overrides PluginSelectorInterface::getSelectedPlugin
PluginSelectorBase::isRequired public function Returns whether a plugin must be selected. Overrides PluginSelectorInterface::isRequired
PluginSelectorBase::resetSelectedPlugin public function Resets the selected plugin. Overrides PluginSelectorInterface::resetSelectedPlugin
PluginSelectorBase::setCollectPluginConfiguration public function Sets whether a plugin's configuration must be collected. Overrides PluginSelectorInterface::setCollectPluginConfiguration
PluginSelectorBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
PluginSelectorBase::setDescription public function Sets the human-readable description. Overrides PluginSelectorInterface::setDescription
PluginSelectorBase::setKeepPreviouslySelectedPlugins public function Sets whether previously selected plugins must be kept. Overrides PluginSelectorInterface::setKeepPreviouslySelectedPlugins
PluginSelectorBase::setLabel public function Sets the human-readable label. Overrides PluginSelectorInterface::setLabel
PluginSelectorBase::setPreviouslySelectedPlugins public function Sets previously selected plugins. Overrides PluginSelectorInterface::setPreviouslySelectedPlugins
PluginSelectorBase::setRequired public function Sets whether a plugin must be selected. Overrides PluginSelectorInterface::setRequired
PluginSelectorBase::setSelectablePluginDiscovery public function Overrides the plugin type's discovery. Overrides PluginSelectorInterface::setSelectablePluginDiscovery
PluginSelectorBase::setSelectablePluginFactory public function Overrides the plugin type's factory. Overrides PluginSelectorInterface::setSelectablePluginFactory
PluginSelectorBase::setSelectablePluginType public function Sets the selectable plugin type. Overrides PluginSelectorInterface::setSelectablePluginType
PluginSelectorBase::setSelectedPlugin public function Sets the selected plugin. Overrides PluginSelectorInterface::setSelectedPlugin
PluginSelectorBase::validateSelectablePluginType protected function Validates the selectable plugin type.
PluginSelectorBase::__construct public function Constructs a new instance. Overrides PluginBase::__construct 1
PluginSelectorInterface::submitSelectorForm public function Submits the selector form. 1
PluginSelectorInterface::validateSelectorForm public function Validates the selector form. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.