You are here

public function FileWidget::massageFormValues in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php \Drupal\file\Plugin\Field\FieldWidget\FileWidget::massageFormValues()
  2. 10 core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php \Drupal\file\Plugin\Field\FieldWidget\FileWidget::massageFormValues()

Massages the form values into the format expected for field values.

Parameters

array $values: The submitted form values produced by the widget.

  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.

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.

Return value

array An array of field values, keyed by delta.

Overrides WidgetBase::massageFormValues

File

core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php, line 277

Class

FileWidget
Plugin implementation of the 'file_generic' widget.

Namespace

Drupal\file\Plugin\Field\FieldWidget

Code

public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {

  // Since file upload widget now supports uploads of more than one file at a
  // time it always returns an array of fids. We have to translate this to a
  // single fid, as field expects single value.
  $new_values = [];
  foreach ($values as &$value) {
    foreach ($value['fids'] as $fid) {
      $new_value = $value;
      $new_value['target_id'] = $fid;
      unset($new_value['fids']);
      $new_values[] = $new_value;
    }
  }
  return $new_values;
}