View source
<?php
namespace Drupal\ueditor\Form;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\Environment;
class UeditorGlobalSettingsForm extends ConfigFormBase {
public function getFormId() {
return 'ueditor_global_settings';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('ueditor.settings');
$form['global']['highlighting_format'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Enable Highlighting Code Format'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_highlighting_format'),
'#description' => t('If enabled, when you insert code, the code will highlighting.'),
);
$form['global']['enable_formula_editor'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Enable Formula in ueditor'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_enable_formula_editor'),
'#description' => $this
->t('If you want to use KityFormula in the ueditor, you must install the @kityformula_link library at first. and the place should be like this: <i>/modules/ueditor/lib/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js</i>', [
'@kityformula_link' => Link::fromTextAndUrl(t('KityFormula'), Url::fromUri('http://ueditor.baidu.com/download/kityformula-plugin.zip'))
->toString(),
]),
);
$form['global']['watermark'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Use watermark when upload'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark'),
'#tree' => FALSE,
'#description' => $this
->t('Check here if you want use watermark when upload.'),
);
$form['global']['watermark_settings'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'input[name="watermark"]' => array(
'checked' => FALSE,
),
),
),
);
$form['global']['watermark_settings']['watermark_type'] = array(
'#type' => 'select',
'#title' => 'The type of watermark',
'#options' => array(
'image' => 'Image',
'text' => 'Text',
),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_type'),
);
$form['global']['watermark_settings']['watermark_image'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'#edit-watermark-type' => array(
'value' => 'text',
),
),
),
);
$form['global']['watermark_settings']['watermark_image']['watermark_path'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Path to custom watermark'),
'#description' => $this
->t('The path to the file you would like to use as watermark image.'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_path'),
);
$validators = array(
'file_validate_extensions' => array(
'jpg png',
),
'file_validate_size' => array(
Environment::getUploadMaxSize(),
),
);
$form['global']['watermark_settings']['watermark_image']['watermark_upload'] = array(
'#type' => 'file',
'#title' => $this
->t('Upload watermark image'),
'#size' => 50,
'#upload_validators' => $validators,
'#description' => $this
->t("If you don't have direct file access to the server, use this field to upload watermark image.only support : jpg png"),
);
$form['global']['watermark_settings']['watermark_image']['watermark_alpha'] = array(
'#type' => 'select',
'#title' => $this
->t('Watermark Alpha'),
'#options' => array_combine(array(
30,
50,
80,
100,
), array(
30,
50,
80,
100,
)),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_alpha'),
);
$form['global']['watermark_settings']['watermark_text'] = array(
'#type' => 'container',
'#states' => array(
'invisible' => array(
'#edit-watermark-type' => array(
'value' => 'image',
),
),
),
);
$form['global']['watermark_settings']['watermark_text']['watermark_textcontent'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Watermark Text'),
'#description' => $this
->t('The path to the file you would like to use as watermark image.'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_textcontent'),
);
$form['global']['watermark_settings']['watermark_text']['watermark_textfontsize'] = array(
'#type' => 'select',
'#title' => $this
->t('Watermark Text Font Size'),
'#options' => array_combine(array(
12,
16,
18,
24,
36,
48,
60,
), array(
12,
16,
18,
24,
36,
48,
60,
)),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_textfontsize'),
);
$form['global']['watermark_settings']['watermark_text']['watermark_textcolor'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Watermark Text Color'),
'#description' => $this
->t('The path to the file you would like to use as watermark image.'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_textcolor'),
);
$form['global']['watermark_settings']['watermark_place'] = array(
'#type' => 'select',
'#title' => $this
->t('Watermark Place'),
'#options' => array(
'0' => 'Random',
'1' => 'Top Left',
'2' => 'Top Center',
'3' => 'Top Right',
'4' => 'Middle Left',
'5' => 'Middle Center',
'6' => 'Middle Right',
'7' => 'Bottom Left',
'8' => 'Bottom Center',
'9' => 'Bottom Right',
),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_watermark_place'),
);
$form['maxsize']['image_size'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Image upload size'),
'#description' => $this
->t('The maxsize of upload image. unit: KB'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_image_maxsize'),
);
$form['maxsize']['file_size'] = array(
'#type' => 'textfield',
'#title' => $this
->t('File upload size'),
'#description' => $this
->t('The maxsize of upload file. unit: KB'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_file_maxsize'),
);
$form['maxsize']['video_size'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Video upload size'),
'#description' => $this
->t('The maxsize of upload video. unit: KB'),
'#default_value' => $config
->get('ueditor_global_settings.ueditor_video_maxsize'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$file = file_save_upload('watermark_upload', $form['global']['watermark_settings']['watermark_image']['watermark_upload']['#upload_validators'], NULL, 0, FileSystemInterface::EXISTS_REPLACE);
if (isset($file)) {
if ($file) {
$form_state
->setValue('watermark_upload', $file);
}
else {
$form_state
->setErrorByName('watermark_upload', $this
->t('The watermark could not be uploaded.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('ueditor.settings');
$values = $form_state
->getValues();
if (file_exists('modules/ueditor/lib/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js')) {
$kityformula_installed = TRUE;
}
else {
$kityformula_installed = FALSE;
}
if ($values['enable_formula_editor'] == 1) {
if (!$kityformula_installed) {
$this
->messenger()
->addError(t('Please install the @kityformula_link library at first, and the place should be like this: <i>modules/ueditor/lib/kityformula-plugin/kityformula/js/kityformula-editor.all.min.js</i>', [
'@kityformula_link' => Link::fromTextAndUrl(t('KityFormula'), Url::fromUri('http://ueditor.baidu.com/download/kityformula-plugin.zip'))
->toString(),
]));
}
else {
$config
->set('ueditor_global_settings.ueditor_enable_formula_editor', $values['enable_formula_editor']);
}
}
if (!empty($values['watermark_upload'])) {
$filename = file_unmanaged_copy($values['watermark_upload']
->getFileUri(), NULL, FileSystemInterface::EXISTS_REPLACE);
$values['watermark_path'] = $filename;
}
unset($values['watermark_upload']);
if (!empty($values['watermark_path'])) {
$values['watermark_path'] = $this
->validatePath($values['watermark_path']);
}
$config
->set('ueditor_global_settings.ueditor_highlighting_format', $values['highlighting_format']);
$config
->set('ueditor_global_settings.ueditor_watermark', $values['watermark']);
$config
->set('ueditor_global_settings.ueditor_watermark_type', $values['watermark_type']);
$config
->set('ueditor_global_settings.ueditor_watermark_path', $values['watermark_path']);
$config
->set('ueditor_global_settings.ueditor_watermark_alpha', $values['watermark_alpha']);
$config
->set('ueditor_global_settings.ueditor_watermark_textcontent', $values['watermark_textcontent']);
$config
->set('ueditor_global_settings.ueditor_watermark_textfontsize', $values['watermark_textfontsize']);
$config
->set('ueditor_global_settings.ueditor_watermark_textcolor', $values['watermark_textcolor']);
$config
->set('ueditor_global_settings.ueditor_watermark_place', $values['watermark_place']);
$config
->set('ueditor_global_settings.ueditor_image_maxsize', $values['image_size']);
$config
->set('ueditor_global_settings.ueditor_file_maxsize', $values['file_size']);
$config
->set('ueditor_global_settings.ueditor_video_maxsize', $values['video_size']);
$config
->save();
}
protected function validatePath($path) {
if (\Drupal::service('file_system')
->realpath($path) == $path) {
return FALSE;
}
if (is_file($path)) {
return $path;
}
if (file_uri_scheme($path) === FALSE) {
$path = 'public://' . $path;
}
if (is_file($path)) {
return $path;
}
return FALSE;
}
protected function getEditableConfigNames() {
return [
'ueditor.settings',
];
}
}