DrupalFile.php in Editor File upload 8
File
src/Plugin/CKEditorPlugin/DrupalFile.php
View source
<?php
namespace Drupal\editor_file\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
class DrupalFile extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
public function getFile() {
return drupal_get_path('module', 'editor_file') . '/js/plugins/drupalfile/plugin.js';
}
public function getLibraries(Editor $editor) {
return [
'core/drupal.ajax',
];
}
public function getConfig(Editor $editor) {
return [
'drupalFile_dialogTitleAdd' => t('Add File'),
'drupalFile_dialogTitleEdit' => t('Edit File'),
];
}
public function getButtons() {
$path = drupal_get_path('module', 'editor_file') . '/js/plugins/drupalfile';
return [
'DrupalFile' => [
'label' => t('File'),
'image' => $path . '/file.png',
],
];
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$form_state
->loadInclude('editor_file', 'admin.inc');
$form['file_upload'] = editor_file_upload_settings_form($editor);
$form['file_upload']['#attached']['library'][] = 'editor_file/drupal.ckeditor.drupalfile.admin';
$form['file_upload']['#element_validate'][] = [
$this,
'validateFileUploadSettings',
];
return $form;
}
public function validateFileUploadSettings(array $element, FormStateInterface $form_state) {
$settings =& $form_state
->getValue($element['#parents']);
$editor = $form_state
->get('editor');
$keys = [
'status',
'scheme',
'directory',
'extensions',
'max_size',
];
foreach ($keys as $key) {
if (array_key_exists($key, $settings)) {
$editor
->setThirdPartySetting('editor_file', $key, $settings[$key]);
}
else {
$editor
->unsetThirdPartySetting('editor_file', $key);
}
}
$form_state
->unsetValue(array_slice($element['#parents'], 0, -1));
}
}