You are here

protected function Radios::buildSelector in Plugin 8.2

Builds the form elements for the actual plugin selector.

Parameters

array $root_element: The plugin's root element.

\Drupal\Core\Form\FormStateInterface $form_state: The form's state.

\Drupal\Component\Plugin\PluginInspectionInterface[] $plugins: The available plugins.

Return value

array The selector's form elements.

Overrides AdvancedPluginSelectorBase::buildSelector

File

src/Plugin/Plugin/PluginSelector/Radios.php, line 33

Class

Radios
Provides a plugin selector using a radio buttons.

Namespace

Drupal\plugin\Plugin\Plugin\PluginSelector

Code

protected function buildSelector(array $root_element, FormStateInterface $form_state, array $plugins) {
  $element = parent::buildSelector($root_element, $form_state, $plugins);

  /** @var \Drupal\Component\Plugin\PluginInspectionInterface[] $plugins */
  $plugin_options = [];
  foreach ($plugins as $plugin) {
    $plugin_definition = $this->selectablePluginType
      ->ensureTypedPluginDefinition($plugin
      ->getPluginDefinition());
    $plugin_options[$plugin
      ->getPluginId()] = $plugin_definition instanceof PluginLabelDefinitionInterface ? $plugin_definition
      ->getLabel() : $plugin_definition
      ->getId();
  }
  natcasesort($plugin_options);
  $element['container']['plugin_id'] = [
    '#ajax' => [
      'callback' => [
        get_class(),
        'ajaxRebuildForm',
      ],
      'effect' => 'fade',
      'event' => 'change',
      'progress' => 'none',
      'trigger_as' => [
        'name' => $element['container']['change']['#name'],
      ],
    ],
    '#attached' => [
      'library' => [
        'plugin/plugin_selector.plugin_radios',
      ],
    ],
    '#default_value' => $this
      ->getSelectedPlugin() ? $this
      ->getSelectedPlugin()
      ->getPluginId() : NULL,
    '#empty_value' => 'select',
    '#options' => $plugin_options,
    '#required' => $this
      ->isRequired(),
    '#title' => $this
      ->getlabel(),
    '#description' => $this
      ->getDescription(),
    '#type' => 'radios',
  ];
  return $element;
}