You are here

public static function FileWidget::value 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::value()

Form API callback. Retrieves the value for the file_generic field element.

This method is assigned as a #value_callback in formElement() method.

File

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

Class

FileWidget
Plugin implementation of the 'file_generic' widget.

Namespace

Drupal\file\Plugin\Field\FieldWidget

Code

public static function value($element, $input, FormStateInterface $form_state) {
  if ($input) {

    // Checkboxes lose their value when empty.
    // If the display field is present make sure its unchecked value is saved.
    if (empty($input['display'])) {
      $input['display'] = $element['#display_field'] ? 0 : 1;
    }
  }

  // We depend on the managed file element to handle uploads.
  $return = ManagedFile::valueCallback($element, $input, $form_state);

  // Ensure that all the required properties are returned even if empty.
  $return += [
    'fids' => [],
    'display' => 1,
    'description' => '',
  ];
  return $return;
}