You are here

protected static function PluginSelector::getPluginSelector in Plugin 8.2

Gets the plugin selector for a field item's elements.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state:

mixed[] $element: The field widget's form elements.

Return value

\Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorInterface

2 calls to PluginSelector::getPluginSelector()
PluginSelector::massageFormValues in src/Plugin/Field/FieldWidget/PluginSelector.php
Massages the form values into the format expected for field values.
PluginSelector::validateFormElement in src/Plugin/Field/FieldWidget/PluginSelector.php
Implements an #element_validate callback.

File

src/Plugin/Field/FieldWidget/PluginSelector.php, line 95

Class

PluginSelector
Provides a plugin selector field widget.

Namespace

Drupal\plugin\Plugin\Field\FieldWidget

Code

protected static function getPluginSelector(FormStateInterface $form_state, array $element) {

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $element['#field_definition'];
  $form_state_key = sprintf('plugin_selector:%s:%d', $field_definition
    ->getName(), $element['#delta']);
  if ($form_state
    ->has($form_state_key)) {
    $plugin_selector = $form_state
      ->get($form_state_key);
  }
  else {

    /** @var \Drupal\plugin\PluginType\PluginTypeManagerInterface $plugin_type_manager */
    $plugin_type_manager = \Drupal::service('plugin.plugin_type_manager');

    /** @var \Drupal\plugin\Plugin\Plugin\PluginSelector\PluginSelectorManagerInterface $plugin_selector_manager */
    $plugin_selector_manager = \Drupal::service('plugin.manager.plugin.plugin_selector');
    $plugin_type = $plugin_type_manager
      ->getPluginType($element['#plugin_type_id']);
    $plugin_selector = $plugin_selector_manager
      ->createInstance($element['#plugin_selector_id']);
    $plugin_selector
      ->setLabel($field_definition
      ->getLabel());
    $plugin_selector
      ->setDescription($field_definition
      ->getDescription());
    $plugin_selector
      ->setRequired($field_definition
      ->isRequired());
    $plugin_selector
      ->setSelectablePluginType($plugin_type);
    $plugin_selector
      ->setKeepPreviouslySelectedPlugins();
    if ($element['#selected_plugin']) {
      $plugin_selector
        ->setSelectedPlugin($element['#selected_plugin']);
    }
    $form_state
      ->set($form_state_key, $plugin_selector);
  }
  return $plugin_selector;
}