function ManagedFile::preRenderManagedFile

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

Render API callback: Hides display of the upload or remove controls.

Upload controls are hidden when a file is already uploaded. Remove controls are hidden when there is no file attached. Controls are hidden here instead of in \Drupal\file\Element\ManagedFile::processManagedFile(), because #access for these buttons depends on the managed_file element's #value. See the documentation of \Drupal\Core\Form\FormBuilderInterface::doBuildForm() for more detailed information about the relationship between #process, #value, and #access.

Because #access is set here, it affects display only and does not prevent JavaScript or other untrusted code from submitting the form as though access were enabled. The form processing functions for these elements should not assume that the buttons can't be "clicked" just because they are not displayed.

See also

\Drupal\file\Element\ManagedFile::processManagedFile()

\Drupal\Core\Form\FormBuilderInterface::doBuildForm()

File

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

Class

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

Namespace

Drupal\file\Element

Code

public static function preRenderManagedFile($element) {
    // If we already have a file, we don't want to show the upload controls.
    if (!empty($element['#value']['fids'])) {
        if (!$element['#multiple']) {
            $element['upload']['#access'] = FALSE;
            $element['upload_button']['#access'] = FALSE;
        }
    }
    else {
        $element['remove_button']['#access'] = FALSE;
    }
    return $element;
}

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