function WidgetBase::extractFormValues

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::extractFormValues()
  2. 10 core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::extractFormValues()
  3. 11.x core/lib/Drupal/Core/Field/WidgetBase.php \Drupal\Core\Field\WidgetBase::extractFormValues()

Overrides WidgetBaseInterface::extractFormValues

2 calls to WidgetBase::extractFormValues()
FileWidget::extractFormValues in core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
Extracts field values from submitted form values.
MediaLibraryWidget::extractFormValues in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Extracts field values from submitted form values.
3 methods override WidgetBase::extractFormValues()
FileWidget::extractFormValues in core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
Extracts field values from submitted form values.
LayoutBuilderWidget::extractFormValues in core/modules/layout_builder/src/Plugin/Field/FieldWidget/LayoutBuilderWidget.php
Extracts field values from submitted form values.
MediaLibraryWidget::extractFormValues in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Extracts field values from submitted form values.

File

core/lib/Drupal/Core/Field/WidgetBase.php, line 374

Class

WidgetBase
Base class for 'Field widget' plugin implementations.

Namespace

Drupal\Core\Field

Code

public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
    $field_name = $this->fieldDefinition
        ->getName();
    // Extract the values from $form_state->getValues().
    $path = array_merge($form['#parents'], [
        $field_name,
    ]);
    $key_exists = NULL;
    $values = NestedArray::getValue($form_state->getValues(), $path, $key_exists);
    if ($key_exists) {
        // Account for drag-and-drop reordering if needed.
        if (!$this->handlesMultipleValues()) {
            // Remove the 'value' of the 'add more' button.
            unset($values['add_more']);
            // The original delta, before drag-and-drop reordering, is needed to
            // route errors to the correct form element.
            foreach ($values as $delta => &$value) {
                $value['_original_delta'] = $delta;
            }
            usort($values, function ($a, $b) {
                return SortArray::sortByKeyInt($a, $b, '_weight');
            });
        }
        // Let the widget massage the submitted values.
        $values = $this->massageFormValues($values, $form, $form_state);
        // Assign the values and remove the empty ones.
        $items->setValue($values);
        $items->filterEmptyItems();
        // Put delta mapping in $form_state, so that flagErrors() can use it.
        $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
        foreach ($items as $delta => $item) {
            $field_state['original_deltas'][$delta] = $item->_original_delta ?? $delta;
            unset($item->_original_delta, $item->_weight);
        }
        static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.