class DropzoneJs in DropzoneJS 8
Same name and namespace in other branches
- 8.2 src/Element/DropzoneJs.php \Drupal\dropzonejs\Element\DropzoneJs
Provides a DropzoneJS atop of the file element.
Required options are:
- #title (string) The main field title.
- #description (string) Description under the field.
- #dropzone_description (string) Will be visible inside the upload area.
- #max_filesize (string) Used by dropzonejs and expressed in number + unit (i.e. 1.1M) This will be converted to a form that DropzoneJs understands. See: http://www.dropzonejs.com/#config-maxFilesize
- #extensions (string) A string of valid extensions separated by a space.
- #max_files (integer) Number of files that can be uploaded. If < 1, there is no limit.
- #clientside_resize (bool) Whether or not to use DropzoneJS clientside resizing. It requires v4.4.0+ version of the library.
Optional options are:
- #resize_width (integer) (optional) The maximum with in px. If omitted defaults to NULL.
- #resize_height (integer) (optional) The maximum height in px. If omitted defaults to NULL.
- #resize_quality (float) (optional) The quality of the resize. Accepts values from 0 - 1. Ie: 0.8. Defautls to 1.
- #resize_method (string) (optional) Accepts 'contain', which scales the image, or 'crop' which crops the image. Defaults to 'contain'.
- #thumbnail_method (string). (optional) Accepts 'contain', which scales the image, or 'crop' which crops the image. Defaults to 'contain'.
@todo Not sure about the version for clientside.
When submitted the element returns an array of temporary file locations. It's the duty of the environment that implements this element to handle the uploaded files.
Plugin annotation
@FormElement("dropzonejs");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\dropzonejs\Element\DropzoneJs
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of DropzoneJs
2 #type uses of DropzoneJs
- DropzoneJsEbWidget::getForm in modules/
eb_widget/ src/ Plugin/ EntityBrowser/ Widget/ DropzoneJsEbWidget.php - DropzoneJsTestForm::buildForm in tests/
modules/ dropzonejs_test/ src/ Form/ DropzoneJsTestForm.php - Form constructor.
File
- src/
Element/ DropzoneJs.php, line 59
Namespace
Drupal\dropzonejs\ElementView source
class DropzoneJs extends FormElement {
/**
* A defualut set of valid extensions.
*/
const DEFAULT_VALID_EXTENSIONS = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#process' => [
[
$class,
'processDropzoneJs',
],
],
'#pre_render' => [
[
$class,
'preRenderDropzoneJs',
],
],
'#theme' => 'dropzonejs',
'#theme_wrappers' => [
'form_element',
],
'#tree' => TRUE,
'#attached' => [
'library' => [
'dropzonejs/integration',
],
],
];
}
/**
* Processes a dropzone upload element.
*/
public static function processDropzoneJs(&$element, FormStateInterface $form_state, &$complete_form) {
$element['uploaded_files'] = [
'#type' => 'hidden',
// @todo Handle defaults.
'#default_value' => '',
// If we send a url with a token through drupalSettings the placeholder
// doesn't get replaced, because the actual scripts markup is not there
// yet. So we pass this information through a data attribute.
'#attributes' => [
'data-upload-path' => Url::fromRoute('dropzonejs.upload')
->toString(),
],
];
if (empty($element['#max_filesize'])) {
$element['#max_filesize'] = file_upload_max_size();
}
// Set #max_files to NULL (explicitly unlimited) if #max_files is not
// specified.
if (empty($element['#max_files'])) {
$element['#max_files'] = NULL;
}
if (!\Drupal::currentUser()
->hasPermission('dropzone upload files')) {
$element['#access'] = FALSE;
drupal_set_message(new TranslatableMarkup("You don't have sufficent permissions to use the DropzoneJS uploader. Contact your system administrator"), 'warning');
}
return $element;
}
/**
* Prepares a #type 'dropzone' render element for dropzonejs.html.twig.
*
* @param array $element
* An associative array containing the properties of the element.
* Properties used: #title, #description, #required, #attributes,
* #dropzone_description, #max_filesize.
*
* @return array
* The $element with prepared variables ready for input.html.twig.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$return['uploaded_files'] = [];
if ($input !== FALSE) {
$user_input = NestedArray::getValue($form_state
->getUserInput(), $element['#parents'] + [
'uploaded_files',
]);
if (!empty($user_input['uploaded_files'])) {
$file_names = array_filter(explode(';', $user_input['uploaded_files']));
$tmp_upload_scheme = \Drupal::configFactory()
->get('dropzonejs.settings')
->get('tmp_upload_scheme');
foreach ($file_names as $name) {
// The upload handler appended the txt extension to the file for
// security reasons. We will remove it in this callback.
$old_filepath = $tmp_upload_scheme . '://' . $name;
// The upload handler appended the txt extension to the file for
// security reasons. Because here we know the acceptable extensions
// we can remove that extension and sanitize the filename.
$name = self::fixTmpFilename($name);
$name = file_munge_filename($name, self::getValidExtensions($element));
// Potentially we moved the file already, so let's check first whether
// we still have to move.
if (file_exists($old_filepath)) {
// Finaly rename the file and add it to results.
$new_filepath = $tmp_upload_scheme . '://' . $name;
$move_result = file_unmanaged_move($old_filepath, $new_filepath);
if ($move_result) {
$return['uploaded_files'][] = [
'path' => $move_result,
'filename' => $name,
];
}
else {
drupal_set_message(self::t('There was a problem while processing the file named @name', [
'@name' => $name,
]), 'error');
}
}
}
}
$form_state
->setValueForElement($element, $return);
}
return $return;
}
/**
* Gets valid file extensions for this element.
*
* @param array $element
* The element array.
*
* @return string
* A space separated list of extensions.
*/
public static function getValidExtensions(array $element) {
return isset($element['#extensions']) ? $element['#extensions'] : self::DEFAULT_VALID_EXTENSIONS;
}
/**
* Fix temporary filename.
*
* The upload handler appended the txt extension to the file for
* security reasons.
*
* @param string $filename
* The filename we need to fix.
*
* @return string
* The fixed filename.
*/
public static function fixTmpFilename($filename) {
$parts = explode('.', $filename);
array_pop($parts);
return implode('.', $parts);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
DropzoneJs:: |
constant | A defualut set of valid extensions. | ||
DropzoneJs:: |
public static | function | Fix temporary filename. | |
DropzoneJs:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
DropzoneJs:: |
public static | function | Gets valid file extensions for this element. | |
DropzoneJs:: |
public static | function | Prepares a #type 'dropzone' render element for dropzonejs.html.twig. | |
DropzoneJs:: |
public static | function | Processes a dropzone upload element. | |
DropzoneJs:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |
|
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |