SourceEditing.php in Drupal 10
File
core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/SourceEditing.php
View source
<?php
declare (strict_types=1);
namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\HTMLRestrictions;
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;
class SourceEditing extends CKEditor5PluginDefault implements CKEditor5PluginConfigurableInterface, CKEditor5PluginElementsSubsetInterface {
use CKEditor5PluginConfigurableTrait;
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['allowed_tags'] = [
'#type' => 'textarea',
'#title' => $this
->t('Manually editable HTML tags'),
'#default_value' => implode(' ', $this->configuration['allowed_tags']),
'#description' => $this
->t('A list of HTML tags that can be used while editing source. It is only necessary to add tags that are not already supported by other enabled plugins. For example, if "Bold" is enabled, it is not necessary to add the <code><strong></code> tag, but it may be necessary to add <code><dl><dt><dd></code> in a format that does not have a definition list plugin, but requires definition list markup.'),
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$form_value = $form_state
->getValue('allowed_tags');
assert(is_string($form_value));
$config_value = HTMLRestrictions::fromString($form_value)
->toCKEditor5ElementsArray();
$form_state
->setValue('allowed_tags', $config_value);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['allowed_tags'] = $form_state
->getValue('allowed_tags');
}
public function defaultConfiguration() {
return [
'allowed_tags' => [],
];
}
public function getElementsSubset() : array {
$r = HTMLRestrictions::fromString(implode(' ', $this->configuration['allowed_tags']));
$plain_tags = $r
->extractPlainTagsSubset()
->toCKEditor5ElementsArray();
$subset = array_unique(array_merge($plain_tags, $this->configuration['allowed_tags']));
return $subset;
}
public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor) : array {
$restrictions = HTMLRestrictions::fromString(implode(' ', $this->configuration['allowed_tags']));
$concrete_restrictions = $restrictions
->getConcreteSubset();
return [
'htmlSupport' => [
'allow' => $concrete_restrictions
->toGeneralHtmlSupportConfig(),
],
];
}
}