ManagedFile::preRenderManagedFile

public static ManagedFile::preRenderManagedFile($element)

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 378

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;
    }
  }
  // If we don't already have a file, there is nothing to remove.
  else {
    $element['remove_button']['#access'] = FALSE;
  }
  return $element;
}
doc_Drupal
2016-10-29 09:25:27
Comments
Leave a Comment

Please login to continue.