public function LibraryItemFieldWidget::extractFormValues in Library 8
Extracts field values from submitted form values.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values. This parameter is altered by reference to receive the incoming form values.
array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Overrides WidgetBase::extractFormValues
File
- src/
Plugin/ Field/ FieldWidget/ LibraryItemFieldWidget.php, line 221
Class
- LibraryItemFieldWidget
- Plugin implementation of the 'library_item_field_widget' widget.
Namespace
Drupal\library\Plugin\Field\FieldWidgetCode
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
if ($this
->isDefaultValueWidget($form_state)) {
$items
->filterEmptyItems();
return;
}
$field_name = $this->fieldDefinition
->getName();
$submittedValues = $form_state
->getValue($field_name);
$sortedEntityReferences = [];
foreach ($items as $delta => $value) {
$formElement = NestedArray::getValue($form, [
$field_name,
'widget',
$delta,
]);
/** @var \Drupal\Core\Entity\EntityInterface $entity */
if (isset($formElement['library']['#entity'], $formElement['library']['#entity']['#value'])) {
$entity = $formElement['library']['#entity']['#value'];
$sortedEntityReferences[$submittedValues[$delta]['_weight']] = [
'target_id' => $entity
->id(),
'entity' => $entity,
];
}
}
ksort($sortedEntityReferences);
$sortedEntityReferences = array_values($sortedEntityReferences);
$items
->setValue($sortedEntityReferences);
$items
->filterEmptyItems();
$widget_state = [
'instance' => $this->fieldDefinition,
'delete' => [],
'entities' => [],
];
foreach ($items as $delta => $value) {
$widget_state['entities'][$delta] = [
'entity' => $value->entity,
'needs_save' => FALSE,
];
}
$form_state
->set('library_item', $widget_state);
}