public static function DropzoneJs::preRenderDropzoneJs in DropzoneJS 8
Same name and namespace in other branches
- 8.2 src/Element/DropzoneJs.php \Drupal\dropzonejs\Element\DropzoneJs::preRenderDropzoneJs()
Prepares a #type 'dropzone' render element for dropzonejs.html.twig.
Parameters
array $element: An associative array containing the properties of the element. Properties used: #title, #description, #required, #attributes, #dropzone_description, #max_filesize.
Return value
array The $element with prepared variables ready for input.html.twig.
File
- src/
Element/ DropzoneJs.php, line 127
Class
- DropzoneJs
- Provides a DropzoneJS atop of the file element.
Namespace
Drupal\dropzonejs\ElementCode
public static function preRenderDropzoneJs(array $element) {
// Convert the human size input to bytes, convert it to MB and round it.
$max_size = round(Bytes::toInt($element['#max_filesize']) / pow(Bytes::KILOBYTE, 2), 2);
$element['#attached']['drupalSettings']['dropzonejs'] = [
'instances' => [
// Configuration keys are matched with DropzoneJS configuration
// options.
$element['#id'] => [
'maxFilesize' => $max_size,
'dictDefaultMessage' => Html::escape($element['#dropzone_description']),
'acceptedFiles' => '.' . str_replace(' ', ',.', self::getValidExtensions($element)),
'maxFiles' => $element['#max_files'],
],
],
];
if (!empty($element['#clientside_resize'])) {
$element['#attached']['drupalSettings']['dropzonejs']['instances'][$element['#id']] += [
'resizeWidth' => !empty($element['#resize_width']) ? $element['#resize_width'] : NULL,
'resizeHeight' => !empty($element['#resize_height']) ? $element['#resize_height'] : NULL,
'resizeQuality' => !empty($element['#resize_quality']) ? $element['#resize_quality'] : 1,
'resizeMethod' => !empty($element['#resize_method']) ? $element['#resize_method'] : 'contain',
'thumbnailMethod' => !empty($element['#thumbnail_method']) ? $element['#thumbnail_method'] : 'contain',
];
array_unshift($element['#attached']['library'], 'dropzonejs/exif-js');
}
static::setAttributes($element, [
'dropzone-enable',
]);
return $element;
}