function FileWidget::massageFormValues

Same name 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()
  3. 11.x core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php \Drupal\file\Plugin\Field\FieldWidget\FileWidget::massageFormValues()

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;
}

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