View source
<?php
namespace Drupal\ckeditor\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginContextualInterface;
use Drupal\ckeditor\CKEditorPluginManager;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Plugin\FilterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Internal extends CKEditorPluginBase implements ContainerFactoryPluginInterface, CKEditorPluginContextualInterface {
protected $cache;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache_backend) {
$this->cache = $cache_backend;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('cache.default'));
}
public function isInternal() {
return TRUE;
}
public function isEnabled(Editor $editor) {
return TRUE;
}
public function getFile() {
return FALSE;
}
public function getConfig(Editor $editor) {
$config = [
'customConfig' => '',
'pasteFromWordPromptCleanup' => TRUE,
'resize_dir' => 'vertical',
'justifyClasses' => [
'text-align-left',
'text-align-center',
'text-align-right',
'text-align-justify',
],
'entities' => FALSE,
'disableNativeSpellChecker' => FALSE,
];
list($config['allowedContent'], $config['disallowedContent']) = $this
->generateACFSettings($editor);
$toolbar_buttons = CKEditorPluginManager::getEnabledButtons($editor);
if (in_array('Format', $toolbar_buttons)) {
$config['format_tags'] = $this
->generateFormatTagsSetting($editor);
}
return $config;
}
public function getButtons() {
$button = function ($name, $direction = 'ltr') {
$class_name = str_replace(' ', '', $name);
return [
'#type' => 'inline_template',
'#template' => '<a href="#" class="cke-icon-only cke_{{ direction }}" role="button" title="{{ name }}" aria-label="{{ name }}"><span class="cke_button_icon cke_button__{{ classname }}_icon">{{ name }}</span></a>',
'#context' => [
'direction' => $direction,
'name' => $name,
'classname' => $class_name,
],
];
};
return [
'Bold' => [
'label' => $this
->t('Bold'),
'image_alternative' => $button('bold'),
'image_alternative_rtl' => $button('bold', 'rtl'),
],
'Italic' => [
'label' => $this
->t('Italic'),
'image_alternative' => $button('italic'),
'image_alternative_rtl' => $button('italic', 'rtl'),
],
'Underline' => [
'label' => $this
->t('Underline'),
'image_alternative' => $button('underline'),
'image_alternative_rtl' => $button('underline', 'rtl'),
],
'Strike' => [
'label' => $this
->t('Strike-through'),
'image_alternative' => $button('strike'),
'image_alternative_rtl' => $button('strike', 'rtl'),
],
'Superscript' => [
'label' => $this
->t('Superscript'),
'image_alternative' => $button('super script'),
'image_alternative_rtl' => $button('super script', 'rtl'),
],
'Subscript' => [
'label' => $this
->t('Subscript'),
'image_alternative' => $button('sub script'),
'image_alternative_rtl' => $button('sub script', 'rtl'),
],
'RemoveFormat' => [
'label' => $this
->t('Remove format'),
'image_alternative' => $button('remove format'),
'image_alternative_rtl' => $button('remove format', 'rtl'),
],
'JustifyLeft' => [
'label' => $this
->t('Align left'),
'image_alternative' => $button('justify left'),
'image_alternative_rtl' => $button('justify left', 'rtl'),
],
'JustifyCenter' => [
'label' => $this
->t('Align center'),
'image_alternative' => $button('justify center'),
'image_alternative_rtl' => $button('justify center', 'rtl'),
],
'JustifyRight' => [
'label' => $this
->t('Align right'),
'image_alternative' => $button('justify right'),
'image_alternative_rtl' => $button('justify right', 'rtl'),
],
'JustifyBlock' => [
'label' => $this
->t('Justify'),
'image_alternative' => $button('justify block'),
'image_alternative_rtl' => $button('justify block', 'rtl'),
],
'BulletedList' => [
'label' => $this
->t('Bullet list'),
'image_alternative' => $button('bulleted list'),
'image_alternative_rtl' => $button('bulleted list', 'rtl'),
],
'NumberedList' => [
'label' => $this
->t('Numbered list'),
'image_alternative' => $button('numbered list'),
'image_alternative_rtl' => $button('numbered list', 'rtl'),
],
'Outdent' => [
'label' => $this
->t('Outdent'),
'image_alternative' => $button('outdent'),
'image_alternative_rtl' => $button('outdent', 'rtl'),
],
'Indent' => [
'label' => $this
->t('Indent'),
'image_alternative' => $button('indent'),
'image_alternative_rtl' => $button('indent', 'rtl'),
],
'Undo' => [
'label' => $this
->t('Undo'),
'image_alternative' => $button('undo'),
'image_alternative_rtl' => $button('undo', 'rtl'),
],
'Redo' => [
'label' => $this
->t('Redo'),
'image_alternative' => $button('redo'),
'image_alternative_rtl' => $button('redo', 'rtl'),
],
'Blockquote' => [
'label' => $this
->t('Blockquote'),
'image_alternative' => $button('blockquote'),
'image_alternative_rtl' => $button('blockquote', 'rtl'),
],
'HorizontalRule' => [
'label' => $this
->t('Horizontal rule'),
'image_alternative' => $button('horizontal rule'),
'image_alternative_rtl' => $button('horizontal rule', 'rtl'),
],
'Cut' => [
'label' => $this
->t('Cut'),
'image_alternative' => $button('cut'),
'image_alternative_rtl' => $button('cut', 'rtl'),
],
'Copy' => [
'label' => $this
->t('Copy'),
'image_alternative' => $button('copy'),
'image_alternative_rtl' => $button('copy', 'rtl'),
],
'Paste' => [
'label' => $this
->t('Paste'),
'image_alternative' => $button('paste'),
'image_alternative_rtl' => $button('paste', 'rtl'),
],
'PasteText' => [
'label' => $this
->t('Paste Text'),
'image_alternative' => $button('paste text'),
'image_alternative_rtl' => $button('paste text', 'rtl'),
],
'PasteFromWord' => [
'label' => $this
->t('Paste from Word'),
'image_alternative' => $button('paste from word'),
'image_alternative_rtl' => $button('paste from word', 'rtl'),
],
'SpecialChar' => [
'label' => $this
->t('Character map'),
'image_alternative' => $button('special char'),
'image_alternative_rtl' => $button('special char', 'rtl'),
],
'Format' => [
'label' => $this
->t('HTML block format'),
'image_alternative' => [
'#type' => 'inline_template',
'#template' => '<a href="#" role="button" aria-label="{{ format_text }}"><span class="ckeditor-button-dropdown">{{ format_text }}<span class="ckeditor-button-arrow"></span></span></a>',
'#context' => [
'format_text' => $this
->t('Format'),
],
],
],
'Table' => [
'label' => $this
->t('Table'),
'image_alternative' => $button('table'),
'image_alternative_rtl' => $button('table', 'rtl'),
],
'ShowBlocks' => [
'label' => $this
->t('Show blocks'),
'image_alternative' => $button('show blocks'),
'image_alternative_rtl' => $button('show blocks', 'rtl'),
],
'Source' => [
'label' => $this
->t('Source code'),
'image_alternative' => $button('source'),
'image_alternative_rtl' => $button('source', 'rtl'),
],
'Maximize' => [
'label' => $this
->t('Maximize'),
'image_alternative' => $button('maximize'),
'image_alternative_rtl' => $button('maximize', 'rtl'),
],
'-' => [
'label' => $this
->t('Separator'),
'image_alternative' => [
'#type' => 'inline_template',
'#template' => '<a href="#" role="button" aria-label="{{ button_separator_text }}" class="ckeditor-separator"></a>',
'#context' => [
'button_separator_text' => $this
->t('Button separator'),
],
],
'attributes' => [
'class' => [
'ckeditor-button-separator',
],
'data-drupal-ckeditor-type' => 'separator',
],
'multiple' => TRUE,
],
];
}
protected function generateFormatTagsSetting(Editor $editor) {
if (!$editor
->hasAssociatedFilterFormat()) {
return [];
}
$format = $editor
->getFilterFormat();
$cid = 'ckeditor_internal_format_tags:' . $format
->id();
if ($cached = $this->cache
->get($cid)) {
$format_tags = $cached->data;
}
else {
$format_tags = [
'p',
];
$possible_format_tags = [
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'pre',
];
foreach ($possible_format_tags as $tag) {
$input = '<' . $tag . '>TEST</' . $tag . '>';
$output = trim(check_markup($input, $editor
->id()));
if (Html::load($output)
->getElementsByTagName($tag)->length !== 0) {
$format_tags[] = $tag;
}
}
$format_tags = implode(';', $format_tags);
$this->cache
->set($cid, $format_tags, Cache::PERMANENT, $format
->getCacheTags());
}
return $format_tags;
}
protected function generateACFSettings(Editor $editor) {
if (!$editor
->hasAssociatedFilterFormat()) {
return TRUE;
}
$format = $editor
->getFilterFormat();
$filter_types = $format
->getFilterTypes();
if (!in_array(FilterInterface::TYPE_HTML_RESTRICTOR, $filter_types)) {
return [
TRUE,
FALSE,
];
}
else {
$get_attribute_values = function ($attribute_values, $allowed_values) {
$values = array_keys(array_filter($attribute_values, function ($value) use ($allowed_values) {
if ($allowed_values) {
return $value !== FALSE;
}
else {
return $value === FALSE;
}
}));
if (count($values)) {
return implode(',', $values);
}
else {
return NULL;
}
};
$html_restrictions = $format
->getHtmlRestrictions();
if ($html_restrictions === FALSE) {
return [
TRUE,
FALSE,
];
}
$allowed = [];
$disallowed = [];
if (isset($html_restrictions['forbidden_tags'])) {
foreach ($html_restrictions['forbidden_tags'] as $tag) {
$disallowed[$tag] = TRUE;
}
}
foreach ($html_restrictions['allowed'] as $tag => $attributes) {
if ($attributes === FALSE) {
$allowed[$tag] = [
'attributes' => FALSE,
'styles' => FALSE,
'classes' => FALSE,
];
}
elseif ($attributes === TRUE) {
$allowed[$tag] = [
'attributes' => TRUE,
'styles' => TRUE,
'classes' => TRUE,
];
if (isset($html_restrictions['allowed']['*'])) {
$wildcard = $html_restrictions['allowed']['*'];
if (isset($wildcard['style'])) {
if (!is_array($wildcard['style'])) {
$allowed[$tag]['styles'] = $wildcard['style'];
}
else {
$allowed_styles = $get_attribute_values($wildcard['style'], TRUE);
if (isset($allowed_styles)) {
$allowed[$tag]['styles'] = $allowed_styles;
}
else {
unset($allowed[$tag]['styles']);
}
}
}
if (isset($wildcard['class'])) {
if (!is_array($wildcard['class'])) {
$allowed[$tag]['classes'] = $wildcard['class'];
}
else {
$allowed_classes = $get_attribute_values($wildcard['class'], TRUE);
if (isset($allowed_classes)) {
$allowed[$tag]['classes'] = $allowed_classes;
}
else {
unset($allowed[$tag]['classes']);
}
}
}
}
}
elseif (is_array($attributes)) {
$allowed[$tag] = [
'attributes' => FALSE,
'styles' => FALSE,
'classes' => FALSE,
];
$allowed_attributes = array_filter($attributes, function ($value) {
return $value !== FALSE;
});
if (count($allowed_attributes)) {
$allowed[$tag]['attributes'] = implode(',', array_keys($allowed_attributes));
}
if (isset($allowed_attributes['style'])) {
if (is_bool($allowed_attributes['style'])) {
$allowed[$tag]['styles'] = $allowed_attributes['style'];
}
elseif (is_array($allowed_attributes['style'])) {
$allowed_classes = $get_attribute_values($allowed_attributes['style'], TRUE);
if (isset($allowed_classes)) {
$allowed[$tag]['styles'] = $allowed_classes;
}
}
}
if (isset($allowed_attributes['class'])) {
if (is_bool($allowed_attributes['class'])) {
$allowed[$tag]['classes'] = $allowed_attributes['class'];
}
elseif (is_array($allowed_attributes['class'])) {
$allowed_classes = $get_attribute_values($allowed_attributes['class'], TRUE);
if (isset($allowed_classes)) {
$allowed[$tag]['classes'] = $allowed_classes;
}
}
}
$disallowed_attributes = array_filter($attributes, function ($value) {
return $value === FALSE;
});
if (count($disallowed_attributes)) {
if (isset($disallowed_attributes['class'])) {
unset($disallowed_attributes['class']);
}
if (isset($disallowed_attributes['style'])) {
unset($disallowed_attributes['style']);
}
$disallowed[$tag]['attributes'] = implode(',', array_keys($disallowed_attributes));
}
if (isset($allowed_attributes['style']) && is_array($allowed_attributes['style'])) {
$disallowed_styles = $get_attribute_values($allowed_attributes['style'], FALSE);
if (isset($disallowed_styles)) {
$disallowed[$tag]['styles'] = $disallowed_styles;
}
}
if (isset($allowed_attributes['class']) && is_array($allowed_attributes['class'])) {
$disallowed_classes = $get_attribute_values($allowed_attributes['class'], FALSE);
if (isset($disallowed_classes)) {
$disallowed[$tag]['classes'] = $disallowed_classes;
}
}
}
}
ksort($allowed);
ksort($disallowed);
return [
$allowed,
$disallowed,
];
}
}
}