function ManagedFile::valueCallback

Same name in other branches
  1. 9 core/modules/file/src/Element/ManagedFile.php \Drupal\file\Element\ManagedFile::valueCallback()
  2. 8.9.x core/modules/file/src/Element/ManagedFile.php \Drupal\file\Element\ManagedFile::valueCallback()
  3. 11.x core/modules/file/src/Element/ManagedFile.php \Drupal\file\Element\ManagedFile::valueCallback()

Overrides FormElementBase::valueCallback

1 call to ManagedFile::valueCallback()
FileWidget::value in core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
Form API callback. Retrieves the value for the file_generic field element.

File

core/modules/file/src/Element/ManagedFile.php, line 62

Class

ManagedFile
Provides an AJAX/progress aware widget for uploading and saving a file.

Namespace

Drupal\file\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
    // Find the current value of this field.
    $fids = !empty($input['fids']) ? explode(' ', $input['fids']) : [];
    foreach ($fids as $key => $fid) {
        $fids[$key] = (int) $fid;
    }
    $force_default = FALSE;
    // Process any input and save new uploads.
    if ($input !== FALSE) {
        $input['fids'] = $fids;
        $return = $input;
        // Uploads take priority over all other values.
        if ($files = file_managed_file_save_upload($element, $form_state)) {
            if ($element['#multiple']) {
                $fids = array_merge($fids, array_keys($files));
            }
            else {
                $fids = array_keys($files);
            }
        }
        else {
            // Check for #filefield_value_callback values.
            // Because FAPI does not allow multiple #value_callback values like it
            // does for #element_validate and #process, this fills the missing
            // functionality to allow File fields to be extended through FAPI.
            if (isset($element['#file_value_callbacks'])) {
                foreach ($element['#file_value_callbacks'] as $callback) {
                    $callback($element, $input, $form_state);
                }
            }
            // Load files if the FIDs have changed to confirm they exist.
            if (!empty($input['fids'])) {
                $fids = [];
                foreach ($input['fids'] as $fid) {
                    if ($file = File::load($fid)) {
                        $fids[] = $file->id();
                        if (!$file->access('download')) {
                            $force_default = TRUE;
                            break;
                        }
                        // Temporary files that belong to other users should never be
                        // allowed.
                        if ($file->isTemporary()) {
                            if ($file->getOwnerId() != \Drupal::currentUser()->id()) {
                                $force_default = TRUE;
                                break;
                            }
                            elseif (\Drupal::currentUser()->isAnonymous()) {
                                $token = NestedArray::getValue($form_state->getUserInput(), array_merge($element['#parents'], [
                                    'file_' . $file->id(),
                                    'fid_token',
                                ]));
                                $file_hmac = Crypt::hmacBase64('file-' . $file->id(), \Drupal::service('private_key')->get() . Settings::getHashSalt());
                                if ($token === NULL || !hash_equals($file_hmac, $token)) {
                                    $force_default = TRUE;
                                    break;
                                }
                            }
                        }
                    }
                }
                if ($force_default) {
                    $fids = [];
                }
            }
        }
    }
    // If there is no input or if the default value was requested above, use the
    // default value.
    if ($input === FALSE || $force_default) {
        if ($element['#extended']) {
            $default_fids = $element['#default_value']['fids'] ?? [];
            $return = $element['#default_value'] ?? [
                'fids' => [],
            ];
        }
        else {
            $default_fids = $element['#default_value'] ?? [];
            $return = [
                'fids' => [],
            ];
        }
        // Confirm that the file exists when used as a default value.
        if (!empty($default_fids)) {
            $fids = [];
            foreach ($default_fids as $fid) {
                if ($file = File::load($fid)) {
                    $fids[] = $file->id();
                }
            }
        }
    }
    $return['fids'] = $fids;
    return $return;
}

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