public function WidgetBase::extractFormValues

public WidgetBase::extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)

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 WidgetBaseInterface::extractFormValues

File

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

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'], array($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] = isset($item->_original_delta) ? $item->_original_delta : $delta;
      unset($item->_original_delta, $item->_weight);
    }
    static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Field!WidgetBase.php/function/WidgetBase::extractFormValues/8.1.x