View source
<?php
namespace Drupal\editor_file\Form;
use Drupal\Component\Utility\Bytes;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\BaseFormIdInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\editor\Ajax\EditorDialogSave;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Component\Utility\Environment;
class EditorFileDialog extends FormBase implements BaseFormIdInterface {
protected $fileStorage;
protected $entityRepository;
public function __construct(EntityStorageInterface $file_storage, EntityRepositoryInterface $entity_repository) {
$this->fileStorage = $file_storage;
$this->entityRepository = $entity_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('file'), $container
->get('entity.repository'));
}
public function getFormId() {
return 'editor_file_dialog';
}
public function getBaseFormId() {
return 'editor_link_dialog';
}
public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
if (isset($form_state
->getUserInput()['editor_object'])) {
$file_element = $form_state
->getUserInput()['editor_object'];
$form_state
->set('file_element', $file_element);
$form_state
->setCached(TRUE);
}
else {
$file_element = $form_state
->get('file_element') ?: [];
}
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'editor/drupal.editor.dialog';
$form['#prefix'] = '<div id="editor-file-dialog-form">';
$form['#suffix'] = '</div>';
$editor = editor_load($filter_format
->id());
$file_upload = $editor
->getThirdPartySettings('editor_file');
$max_filesize = isset($file_upload['max_size']) ? min(Bytes::toInt($file_upload['max_size']), Environment::getUploadMaxSize()) : Environment::getUploadMaxSize();
$existing_file = isset($file_element['data-entity-uuid']) ? $this->entityRepository
->loadEntityByUuid('file', $file_element['data-entity-uuid']) : NULL;
$fid = $existing_file ? $existing_file
->id() : NULL;
$form['fid'] = [
'#title' => $this
->t('File'),
'#type' => 'managed_file',
'#upload_location' => $file_upload['scheme'] . '://' . $file_upload['directory'],
'#default_value' => $fid ? [
$fid,
] : NULL,
'#upload_validators' => [
'file_validate_extensions' => !empty($file_upload['extensions']) ? [
$file_upload['extensions'],
] : [
'txt',
],
'file_validate_size' => [
$max_filesize,
],
],
'#required' => TRUE,
];
$form['attributes']['href'] = [
'#title' => $this
->t('URL'),
'#type' => 'textfield',
'#default_value' => isset($file_element['href']) ? $file_element['href'] : '',
'#maxlength' => 2048,
'#required' => TRUE,
];
if ($file_upload['status']) {
$form['attributes']['href']['#access'] = FALSE;
$form['attributes']['href']['#required'] = FALSE;
}
else {
$form['fid']['#access'] = FALSE;
$form['fid']['#required'] = FALSE;
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['save_modal'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#submit' => [],
'#ajax' => [
'callback' => '::submitForm',
'event' => 'click',
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$fid = $form_state
->getValue([
'fid',
0,
]);
if (!empty($fid)) {
$file = $this->fileStorage
->load($fid);
$file_url = file_create_url($file
->getFileUri());
$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 = [
'file',
'file--mime-' . strtr($mime_type, [
'/' => '-',
'.' => '-',
]),
'file--' . file_icon_class($mime_type),
];
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;
}
}