View source
<?php
namespace Drupal\plugin\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
class PluginSelector extends WidgetBase {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item = $items[$delta];
$plugin_type = $item
->getPluginType();
$element = [
'#delta' => $delta,
'#field_definition' => $this->fieldDefinition,
'#element_validate' => [
[
get_class(),
'validateFormElement',
],
],
'#plugin_type_id' => $plugin_type
->getId(),
'#plugin_selector_id' => $this->pluginDefinition['plugin_selector_id'],
'#process' => [
[
get_class(),
'processFormElement',
],
],
'#selected_plugin' => $items
->isEmpty() ? NULL : $items
->get($delta)
->getContainedPluginInstance(),
];
$element['plugin_selector'] = static::getPluginSelector($form_state, $element)
->buildSelectorForm([], $form_state);
return $element;
}
public static function processFormElement(array &$element, FormStateInterface $form_state, array &$form) {
$element['array_parents'] = [
'#type' => 'value',
'#value' => $element['#array_parents'],
];
return $element;
}
public static function validateFormElement(array &$element, FormStateInterface $form_state, array &$form) {
$plugin_selector = static::getPluginSelector($form_state, $element);
$plugin_selector
->validateSelectorForm($element['plugin_selector'], $form_state);
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$massaged_values = [];
foreach ($values as $delta => $item_values) {
$element = NestedArray::getValue($form, array_slice($item_values['array_parents'], count($form['#array_parents'])));
$plugin_selector = static::getPluginSelector($form_state, $element);
$plugin_selector
->submitSelectorForm($element['plugin_selector'], $form_state);
$massaged_values[$delta] = [
'plugin_instance' => $plugin_selector
->getSelectedPlugin(),
];
}
return $massaged_values;
}
protected static function getPluginSelector(FormStateInterface $form_state, array $element) {
$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 {
$plugin_type_manager = \Drupal::service('plugin.plugin_type_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;
}
}