public static function PlUploadFile::preRenderPlUploadFile in Plupload integration 2.0.x
Same name and namespace in other branches
- 8 src/Element/PlUploadFile.php \Drupal\plupload\Element\PlUploadFile::preRenderPlUploadFile()
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.
Note: based on plupload_element_pre_render().
See also
\Drupal\file\Element\ManagedFile::processManagedFile()
\Drupal\Core\Form\FormBuilderInterface::doBuildForm()
File
- src/
Element/ PlUploadFile.php, line 192
Class
- PlUploadFile
- Provides a PLUpload widget for uploading and saving files.
Namespace
Drupal\plupload\ElementCode
public static function preRenderPlUploadFile($element) {
$settings = isset($element['#plupload_settings']) ? $element['#plupload_settings'] : [];
// Set upload URL.
if (empty($settings['url'])) {
$settings['url'] = Url::fromRoute('plupload.upload', [], [
'query' => [
'token' => \Drupal::csrfToken()
->get('plupload-handle-uploads'),
],
])
->toString();
}
// The Plupload library supports client-side validation of file extension,
// so pass along the information for it to do that. However, as with all
// client- side validation, this is a UI enhancement only, and not a
// replacement for server-side validation.
if (empty($settings['filters']) && isset($element['#upload_validators']['file_validate_extensions'][0])) {
$settings['filters'][] = [
// @todo Some runtimes (e.g., flash) require a non-empty title for each
// filter, but I don't know what this title is used for. Seems a shame
// to hard-code it, but what's a good way to avoid that?
'title' => t('Allowed files'),
'extensions' => str_replace(' ', ',', $element['#upload_validators']['file_validate_extensions'][0]),
];
}
// Check for autoupload and autosubmit settings and add appropriate
// callback.
if (!empty($element['#autoupload'])) {
$settings['init']['FilesAdded'] = 'Drupal.plupload.filesAddedCallback';
if (!empty($element['#autosubmit'])) {
$settings['init']['UploadComplete'] = 'Drupal.plupload.uploadCompleteCallback';
}
}
// Add a specific submit element that we want to click if one is specified.
if (!empty($element['#submit_element'])) {
$settings['submit_element'] = $element['#submit_element'];
}
// Check if there are event callbacks & append them to current ones, if any.
if (!empty($element['#event_callbacks'])) {
// array_merge() only accepts parameters of type array.
if (!isset($settings['init'])) {
$settings['init'] = [];
}
$settings['init'] = array_merge($settings['init'], $element['#event_callbacks']);
}
if (empty($element['#description'])) {
$element['#description'] = '';
}
$element['#description'] = [
'#theme' => 'file_upload_help',
'#description' => $element['#description'],
'#upload_validators' => $element['#upload_validators'],
];
// Global settings.
$library_discovery = \Drupal::service('library.discovery');
$library = $library_discovery
->getLibraryByName('plupload', 'plupload');
$element['#attached']['drupalSettings']['plupload'] = [
'_default' => $library['settings']['plupload']['_default'],
$element['#id'] => $settings,
];
return $element;
}