You are here

class AdvancedPluginSelectorBasePluginSelectorForm in Plugin 8.2

Provides a form to test plugin selector plugins based on AdvancedPluginSelectorBase.

Hierarchy

Expanded class hierarchy of AdvancedPluginSelectorBasePluginSelectorForm

1 string reference to 'AdvancedPluginSelectorBasePluginSelectorForm'
plugin_test_helper.routing.yml in tests/modules/plugin_test_helper/plugin_test_helper.routing.yml
tests/modules/plugin_test_helper/plugin_test_helper.routing.yml

File

tests/modules/plugin_test_helper/src/AdvancedPluginSelectorBasePluginSelectorForm.php, line 18

Namespace

Drupal\plugin_test_helper
View source
class AdvancedPluginSelectorBasePluginSelectorForm implements ContainerInjectionInterface, FormInterface {
  use DependencySerializationTrait;

  /**
   * The plugin selector manager.
   *
   * @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface
   */
  protected $pluginSelectorManager;

  /**
   * A selectable plugin type.
   *
   * @var \Drupal\plugin\PluginType\PluginTypeInterface
   */
  protected $selectablePluginType;

  /**
   * Constructs a new class instance.
   */
  function __construct(PluginTypeInterface $selectable_plugin_type, PluginSelectorManagerInterface $plugin_selector_manager) {
    $this->selectablePluginType = $selectable_plugin_type;
    $this->pluginSelectorManager = $plugin_selector_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /** @var \Drupal\plugin\PluginType\PluginTypeManagerInterface $plugin_type_manager */
    $plugin_type_manager = $container
      ->get('plugin.plugin_type_manager');
    return new static($plugin_type_manager
      ->getPluginType('plugin_test_helper_mock'), $container
      ->get('plugin.manager.plugin.plugin_selector'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'plugin_test_helper_advanced_plugin_selector_base';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $allowed_selectable_plugin_ids = NULL, $plugin_id = NULL, $tree = FALSE, $always_show_selector = FALSE) {
    if ($form_state
      ->has('plugin_selector')) {
      $plugin_selector = $form_state
        ->get('plugin_selector');
    }
    else {
      $selectable_plugin_discovery = new LimitedPluginDiscoveryDecorator($this->selectablePluginType
        ->getPluginManager());
      $selectable_plugin_discovery
        ->setDiscoveryLimit(explode(',', $allowed_selectable_plugin_ids));
      $selectable_plugin_manager = new PluginManagerDecorator($this->selectablePluginType
        ->getPluginManager(), $selectable_plugin_discovery);

      /** @var \Drupal\plugin\Plugin\Plugin\PluginSelector\AdvancedPluginSelectorBase $plugin_selector */
      $plugin_selector = $this->pluginSelectorManager
        ->createInstance($plugin_id);
      $plugin_selector
        ->setSelectablePluginType($this->selectablePluginType);
      $plugin_selector
        ->setSelectablePluginDiscovery($selectable_plugin_manager);
      $plugin_selector
        ->setSelectablePluginFactory($selectable_plugin_manager);
      $plugin_selector
        ->setRequired();
      $plugin_selector
        ->setSelectorVisibilityForSingleAvailability($always_show_selector);
      $form_state
        ->set('plugin_selector', $plugin_selector);
    }
    $form['plugin'] = $plugin_selector
      ->buildSelectorForm([], $form_state);

    // Nest the selector in a tree if that's required.
    if ($tree) {
      $form['tree'] = [
        '#tree' => TRUE,
      ];
      $form['tree']['plugin'] = $form['plugin'];
      unset($form['plugin']);
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => t('Submit'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface $plugin_selector */
    $plugin_selector = $form_state
      ->get('plugin_selector');
    $plugin_form = isset($form['tree']) ? $form['tree']['plugin'] : $form['plugin'];
    $plugin_selector
      ->validateSelectorForm($plugin_form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface $plugin_selector */
    $plugin_selector = $form_state
      ->get('plugin_selector');
    $plugin_form = isset($form['tree']) ? $form['tree']['plugin'] : $form['plugin'];
    $plugin_selector
      ->submitSelectorForm($plugin_form, $form_state);
    \Drupal::state()
      ->set($this
      ->getFormId(), $plugin_selector
      ->getSelectedPlugin());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdvancedPluginSelectorBasePluginSelectorForm::$pluginSelectorManager protected property The plugin selector manager.
AdvancedPluginSelectorBasePluginSelectorForm::$selectablePluginType protected property A selectable plugin type.
AdvancedPluginSelectorBasePluginSelectorForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
AdvancedPluginSelectorBasePluginSelectorForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
AdvancedPluginSelectorBasePluginSelectorForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AdvancedPluginSelectorBasePluginSelectorForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
AdvancedPluginSelectorBasePluginSelectorForm::validateForm public function Form validation handler. Overrides FormInterface::validateForm
AdvancedPluginSelectorBasePluginSelectorForm::__construct function Constructs a new class instance.
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