public static function DropzoneJs::valueCallback in DropzoneJS 8
Same name and namespace in other branches
- 8.2 src/Element/DropzoneJs.php \Drupal\dropzonejs\Element\DropzoneJs::valueCallback()
Determines how user input is mapped to an element's #value property.
Parameters
array $element: An associative array containing the properties of the element.
mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
mixed The value to assign to the element.
Overrides FormElement::valueCallback
File
- src/
Element/ DropzoneJs.php, line 162
Class
- DropzoneJs
- Provides a DropzoneJS atop of the file element.
Namespace
Drupal\dropzonejs\ElementCode
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;
}