You are here

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

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\PluginSelector
View 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

Namesort descending Modifiers Type Description Overrides
AdvancedPluginSelectorBase::ajaxRebuildForm public static function Implements form AJAX callback.
AdvancedPluginSelectorBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
AdvancedPluginSelectorBase::buildMultipleAvailablePlugins public function Builds the form elements for multiple plugins.
AdvancedPluginSelectorBase::buildNoAvailablePlugins public function Builds the form elements for when there are no available plugins.
AdvancedPluginSelectorBase::buildOneAvailablePlugin public function Builds the form elements for one plugin.
AdvancedPluginSelectorBase::buildPluginForm protected function Builds the plugin configuration form elements.
AdvancedPluginSelectorBase::buildSelector protected function Builds the form elements for the actual plugin selector. 2
AdvancedPluginSelectorBase::buildSelectorForm public function Builds the selector form. Overrides PluginSelectorBase::buildSelectorForm 1
AdvancedPluginSelectorBase::create public static function Creates an instance of the plugin. Overrides PluginSelectorBase::create
AdvancedPluginSelectorBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides PluginSelectorBase::defaultConfiguration
AdvancedPluginSelectorBase::getPluginSelector protected static function Gets a plugin selector from the form state.
AdvancedPluginSelectorBase::getSelectorVisibilityForSingleAvailability public function Gets whether or not to show the selection elements for single plugins.
AdvancedPluginSelectorBase::processBuildSelectorForm public static function Implements a Form API #process callback.
AdvancedPluginSelectorBase::rebuildForm public static function Implements form API's #submit.
AdvancedPluginSelectorBase::setPluginSelector protected static function Stores a plugin selector in the form state.
AdvancedPluginSelectorBase::setSelectorVisibilityForSingleAvailability public function Toggles whether or not to show the selection elements for single plugins.
AdvancedPluginSelectorBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
AdvancedPluginSelectorBase::submitSelectorForm public function Submits the selector form. Overrides PluginSelectorInterface::submitSelectorForm
AdvancedPluginSelectorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
AdvancedPluginSelectorBase::validateSelectorForm public function Validates the selector form. Overrides PluginSelectorInterface::validateSelectorForm
AdvancedPluginSelectorBase::__construct public function Constructs a new class instance. Overrides PluginSelectorBase::__construct
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::calculateDependencies public function
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.
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.