public function EditorFileDialog::submitForm in Editor File upload 8
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ EditorFileDialog.php, line 170
Class
- EditorFileDialog
- Provides a link dialog for text editors.
Namespace
Drupal\editor_file\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
// Convert any uploaded files from the FID values to data-entity-uuid
// attributes and set data-entity-type to 'file'.
$fid = $form_state
->getValue([
'fid',
0,
]);
if (!empty($fid)) {
$file = $this->fileStorage
->load($fid);
$file_url = file_create_url($file
->getFileUri());
// Transform absolute file URLs to relative file URLs: prevent problems
// on multisite set-ups and prevent mixed content errors.
$file_url = file_url_transform_relative($file_url);
$form_state
->setValue([
'attributes',
'href',
], $file_url);
$form_state
->setValue([
'attributes',
'data-entity-uuid',
], $file
->uuid());
$form_state
->setValue([
'attributes',
'data-entity-type',
], 'file');
$mime_type = $file
->getMimeType();
// Classes to add to the file field for icons.
$classes = [
'file',
// Add a specific class for each and every mime type.
'file--mime-' . strtr($mime_type, [
'/' => '-',
'.' => '-',
]),
// Add a more general class for groups of well known MIME types.
'file--' . file_icon_class($mime_type),
];
// Merge with existing classes (eg: those added w/ Editor Advanced Link).
if (!empty($form_state
->getValue('attributes')['class'])) {
$existing_classes = preg_split('/\\s+/', $form_state
->getValue('attributes')['class']);
$classes = array_unique(array_merge($existing_classes, $classes));
}
$form_state
->setValue([
'attributes',
'class',
], implode(' ', $classes));
}
if ($form_state
->getErrors()) {
unset($form['#prefix'], $form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$response
->addCommand(new HtmlCommand('#editor-file-dialog-form', $form));
}
else {
$response
->addCommand(new EditorDialogSave($form_state
->getValues()));
$response
->addCommand(new CloseModalDialogCommand());
}
return $response;
}