Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ManagedFile.php

function 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 399

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;
}
RSS feed
Powered by Drupal