function File::valueCallback

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/File.php \Drupal\Core\Render\Element\File::valueCallback()
  2. 10 core/lib/Drupal/Core/Render/Element/File.php \Drupal\Core\Render\Element\File::valueCallback()

Overrides FormElementBase::valueCallback

File

core/lib/Drupal/Core/Render/Element/File.php, line 85

Class

File
Provides a form element for uploading a file.

Namespace

Drupal\Core\Render\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    if ($input === FALSE) {
        return NULL;
    }
    $parents = $element['#parents'];
    $element_name = array_shift($parents);
    $uploaded_files = \Drupal::request()->files
        ->get('files', []);
    $uploaded_file = $uploaded_files[$element_name] ?? NULL;
    if ($uploaded_file) {
        // Cast this to an array so that the structure is consistent regardless of
        // whether #value is set or not.
        return (array) $uploaded_file;
    }
    return NULL;
}

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