View source
<?php
declare (strict_types=1);
namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableTrait;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface;
use Drupal\ckeditor5\Plugin\CKEditor5PluginElementsSubsetInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\EditorInterface;
use Drupal\ckeditor5\HTMLRestrictions;
class Alignment extends CKEditor5PluginDefault implements CKEditor5PluginConfigurableInterface, CKEditor5PluginElementsSubsetInterface {
use CKEditor5PluginConfigurableTrait;
const DEFAULT_CONFIGURATION = [
'enabled_alignments' => [
'left',
'center',
'right',
'justify',
],
];
public function defaultConfiguration() {
return static::DEFAULT_CONFIGURATION;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['enabled_alignments'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Enabled Alignments'),
'#description' => $this
->t('These are the alignment types that will appear in the alignment dropdown.'),
];
foreach ($this
->getPluginDefinition()
->getCKEditor5Config()['alignment']['options'] as $alignment_option) {
$name = $alignment_option['name'];
$form['enabled_alignments'][$name] = [
'#type' => 'checkbox',
'#title' => $this
->t($name),
'#return_value' => $name,
'#default_value' => in_array($name, $this->configuration['enabled_alignments'], TRUE) ? $name : NULL,
];
}
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$form_value = $form_state
->getValue('enabled_alignments');
$config_value = array_values(array_filter($form_value));
$form_state
->setValue('enabled_alignments', $config_value);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['enabled_alignments'] = $form_state
->getValue('enabled_alignments');
}
public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor) : array {
$enabled_alignments = $this->configuration['enabled_alignments'];
$all_alignment_options = $static_plugin_config['alignment']['options'];
$configured_alignment_options = array_filter($all_alignment_options, function ($option) use ($enabled_alignments) {
return in_array($option['name'], $enabled_alignments, TRUE);
});
return [
'alignment' => [
'options' => array_values($configured_alignment_options),
],
];
}
public function getElementsSubset() : array {
$enabled_alignments = $this->configuration['enabled_alignments'];
$plugin_definition = $this
->getPluginDefinition();
$all_elements = $plugin_definition
->getElements();
$subset = HTMLRestrictions::fromString(implode($all_elements));
foreach ($plugin_definition
->getCKEditor5Config()['alignment']['options'] as $configured_alignment) {
if (!in_array($configured_alignment['name'], $enabled_alignments, TRUE)) {
$element_string = '<$text-container class=' . '"' . $configured_alignment["className"] . '"' . '>';
$subset = $subset
->diff(HTMLRestrictions::fromString($element_string));
}
}
return $subset
->toCKEditor5ElementsArray();
}
}