public function WidgetBase::extractFormValues

Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues().

Overrides WidgetBaseInterface::extractFormValues

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetBase.php, line 298
Definition of Drupal\field\Plugin\Type\Widget\WidgetBase.

Class

WidgetBase
Base class for 'Field widget' plugin implementations.

Namespace

Drupal\field\Plugin\Type\Widget

Code

public function extractFormValues(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state) {
  $field_name = $this->field['field_name'];

  // Extract the values from $form_state['values'].
  $path = array_merge($form['#parents'], array(
    $field_name,
    $langcode,
  ));
  $key_exists = NULL;
  $values = NestedArray::getValue($form_state['values'], $path, $key_exists);
  if ($key_exists) {

    // Remove the 'value' of the 'add more' button.
    unset($values['add_more']);

    // Let the widget turn the submitted values into actual field values.
    // Make sure the '_weight' entries are persisted in the process.
    $weights = array();
    if (isset($values[0]['_weight'])) {
      foreach ($values as $delta => $value) {
        $weights[$delta] = $value['_weight'];
      }
    }
    $items = $this
      ->massageFormValues($values, $form, $form_state);
    foreach ($items as $delta => &$item) {

      // Put back the weight.
      if (isset($weights[$delta])) {
        $item['_weight'] = $weights[$delta];
      }

      // The tasks below are going to reshuffle deltas. Keep track of the
      // original deltas for correct reporting of errors in flagErrors().
      $item['_original_delta'] = $delta;
    }

    // Account for drag-n-drop reordering.
    $this
      ->sortItems($items);

    // Remove empty values.
    $items = _field_filter_items($this->field, $items);

    // Put delta mapping in $form_state, so that flagErrors() can use it.
    $field_state = field_form_get_state($form['#parents'], $field_name, $langcode, $form_state);
    foreach ($items as $delta => &$item) {
      $field_state['original_deltas'][$delta] = $item['_original_delta'];
      unset($item['_original_delta']);
    }
    field_form_set_state($form['#parents'], $field_name, $langcode, $form_state, $field_state);
  }
}