View source
<?php
namespace Drupal\bueditor\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
class BUEditorEditorForm extends EntityForm {
public function form(array $form, FormStateInterface $form_state) {
$bueditor_editor = $this
->getEntity();
if ($this
->getOperation() === 'duplicate') {
$bueditor_editor = $bueditor_editor
->createDuplicate();
$bueditor_editor
->set('label', $this
->t('Duplicate of @label', [
'@label' => $bueditor_editor
->label(),
]));
$this
->setEntity($bueditor_editor);
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#default_value' => $bueditor_editor
->label(),
'#maxlength' => 64,
'#required' => TRUE,
'#weight' => -20,
];
$form['id'] = [
'#type' => 'machine_name',
'#machine_name' => [
'exists' => [
get_class($bueditor_editor),
'load',
],
'source' => [
'label',
],
],
'#default_value' => $bueditor_editor
->id(),
'#maxlength' => 32,
'#required' => TRUE,
'#weight' => -20,
];
$form['description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $bueditor_editor
->get('description'),
'#weight' => -10,
];
$widget = $this
->getToolbarWidget();
$widget_libraries = $widget['libraries'];
unset($widget['libraries']);
$form['toolbar_config'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Toolbar configuration'),
'#attached' => [
'library' => $widget_libraries,
'drupalSettings' => [
'bueditor' => [
'twSettings' => $widget,
],
],
],
'#weight' => -6,
];
$form['toolbar_config']['toolbar'] = [
'#type' => 'textfield',
'#title' => $this
->t('Active toolbar'),
'#default_value' => implode(', ', $bueditor_editor
->getToolbar()),
'#attributes' => [
'class' => [
'bueditor-toolbar-input',
],
],
'#maxlength' => NULL,
'#parents' => [
'settings',
'toolbar',
],
];
$form['settings'] = [
'#tree' => TRUE,
'#type' => 'details',
'#title' => $this
->t('Settings'),
'#weight' => -5,
];
$form['settings']['cname'] = [
'#type' => 'textfield',
'#title' => $this
->t('Class name'),
'#default_value' => $bueditor_editor
->getSettings('cname'),
'#description' => $this
->t('Additional class name for the editor element.'),
'#weight' => -8,
];
$form['settings']['indent'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable indentation'),
'#default_value' => $bueditor_editor
->getSettings('indent'),
'#description' => $this
->t('Enable 2 spaces indent by <kbd>TAB</kbd>, unindent by <kbd>Shift+TAB</kbd>, and auto-indent by <kbd>ENTER</kbd>. Once enabled it can be turned on/off dynamically by <kbd>Ctrl+Alt+TAB</kbd>.'),
'#weight' => -7,
];
$form['settings']['acTags'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Autocomplete HTML tags'),
'#default_value' => $bueditor_editor
->getSettings('acTags'),
'#description' => $this
->t('Automatically insert html closing tags.'),
'#weight' => -6,
];
$form['settings']['fileBrowser'] = [
'#type' => 'select',
'#title' => $this
->t('File browser'),
'#options' => [],
'#empty_value' => '',
'#default_value' => $bueditor_editor
->getSettings('fileBrowser'),
'#description' => $this
->t('File browser to use in default image/link dialogs.'),
'#weight' => -5,
];
if (!$bueditor_editor
->isNew()) {
$attached['library'] = $bueditor_editor
->getLibraries();
$attached['drupalSettings']['bueditor']['demoSettings'] = $bueditor_editor
->getJSSettings();
$form['demo'] = [
'#type' => 'text_format',
'#base_type' => 'textarea',
'#title' => $this
->t('Demo'),
'#weight' => 1000,
'#attributes' => [
'class' => [
'bueditor-demo',
],
],
'#attached' => $attached,
'#editor' => FALSE,
'#input' => FALSE,
'#value' => NULL,
];
}
$form['#attached']['library'][] = 'bueditor/drupal.bueditor.admin';
\Drupal::service('plugin.manager.bueditor.plugin')
->alterEditorForm($form, $form_state, $bueditor_editor);
return parent::form($form, $form_state);
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$bueditor_editor = $this
->getEntity();
$toolbar =& $form_state
->getValue([
'settings',
'toolbar',
]);
if (is_string($toolbar)) {
$toolbar = array_values(array_filter(array_map('trim', explode(',', $toolbar))));
}
if (is_string($bueditor_editor
->getToolbar())) {
$bueditor_editor
->setToolbar($toolbar);
}
$cname = $form_state
->getValue([
'settings',
'cname',
]);
if (!empty($cname) && preg_match('/[^a-zA-Z0-9\\-_ ]/', $cname)) {
$form_state
->setError($form['settings']['cname'], $this
->t('Class name is invalid.'));
}
\Drupal::service('plugin.manager.bueditor.plugin')
->validateEditorForm($form, $form_state, $bueditor_editor);
}
public function save(array $form, FormStateInterface $form_state) {
$bueditor_editor = $this
->getEntity();
$status = $bueditor_editor
->save();
if ($status == SAVED_NEW) {
$this
->messenger()
->addMessage($this
->t('Editor %name has been added.', [
'%name' => $bueditor_editor
->label(),
]));
}
elseif ($status == SAVED_UPDATED) {
$this
->messenger()
->addMessage($this
->t('The changes have been saved.'));
}
$form_state
->setRedirect('entity.bueditor_editor.edit_form', [
'bueditor_editor' => $bueditor_editor
->id(),
]);
}
public static function getToolbarWidget() {
$pm = \Drupal::service('plugin.manager.bueditor.plugin');
$widget = [
'items' => $pm
->getButtons(),
'libraries' => [
'bueditor/drupal.bueditor.admin',
],
];
$pm
->alterToolbarWidget($widget);
return $widget;
}
}