Plugin Selector

Component ID

2473823

Component name

Plugin Selector

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Downloads

258

Component created

Component changed

Component body

This module has been replaced by the Plugin module.

Plugin selectors allow users to select plugins of a particular type and configure them using the plugins' configuration forms.

This module is a tool for other modules to build user interfaces. Using plugin selectors, your users can easily select and configure your plugins and you won't have to write any complex form API integration; the plugin selectors will do that for you.

Usage

In any form, do the following:


  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['plugin'] = $this->getPluginSelector($form_state)->buildSelectorForm([], $form_state);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array $form, FormStateInterface $form_state) {
    $this->getPluginSelector($form_state)->validateSelectorForm([], $form_state);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array $form, FormStateInterface $form_state) {
    $this->getPluginSelector($form_state)->submitSelectorForm([], $form_state);

    $plugin = $this->getPluginSelector($form_state)->getSelectedPlugin();

    return $form;
  }

  /**
   * Gets the plugin selector.
   *
   * @param DrupalCoreFormFormStateInterface $form_state
   *
   * @return DrupalpaymentPluginPaymentPluginSelectorPluginSelectorInterface
   */
  protected function getPluginSelector(FormStateInterface $form_state) {
    if (!$form_state->has('plugin_selector')) {
      $plugin_selector_id = 'plugin_selector_select_list';
      $mapper = new DefaultPluginDefinitionMapper();
      $plugin_selector = $this->pluginSelectorManager->createInstance($plugin_selector_id)
        ->setPluginManager($this->selectablePluginManager, $mapper)
        ->setRequired()
        ->setLabel($this->t('Select a plugin'));
      $form_state->set('plugin_selector', $plugin_selector);
    }

    return $form_state->get('plugin_selector');
  }