View source
<?php
namespace Drupal\ckeditor\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
class StylesCombo extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
public function isInternal() {
return TRUE;
}
public function getFile() {
return FALSE;
}
public function getConfig(Editor $editor) {
$config = array();
$settings = $editor
->getSettings();
if (!isset($settings['plugins']['stylescombo']['styles'])) {
return $config;
}
$styles = $settings['plugins']['stylescombo']['styles'];
$config['stylesSet'] = $this
->generateStylesSetSetting($styles);
return $config;
}
public function getButtons() {
return array(
'Styles' => array(
'label' => t('Font style'),
'image_alternative' => [
'#type' => 'inline_template',
'#template' => '<a href="#" role="button" aria-label="{{ styles_text }}"><span class="ckeditor-button-dropdown">{{ styles_text }}<span class="ckeditor-button-arrow"></span></span></a>',
'#context' => [
'styles_text' => t('Styles'),
],
],
),
);
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$config = array(
'styles' => '',
);
$settings = $editor
->getSettings();
if (isset($settings['plugins']['stylescombo'])) {
$config = $settings['plugins']['stylescombo'];
}
$form['styles'] = array(
'#title' => t('Styles'),
'#title_display' => 'invisible',
'#type' => 'textarea',
'#default_value' => $config['styles'],
'#description' => t('A list of classes that will be provided in the "Styles" dropdown. Enter one or more classes on each line in the format: element.classA.classB|Label. Example: h1.title|Title. Advanced example: h1.fancy.title|Fancy title.<br />These styles should be available in your theme\'s CSS file.'),
'#attached' => array(
'library' => array(
'ckeditor/drupal.ckeditor.stylescombo.admin',
),
),
'#element_validate' => array(
array(
$this,
'validateStylesValue',
),
),
);
return $form;
}
public function validateStylesValue(array $element, FormStateInterface $form_state) {
if ($this
->generateStylesSetSetting($element['#value']) === FALSE) {
$form_state
->setError($element, t('The provided list of styles is syntactically incorrect.'));
}
}
protected function generateStylesSetSetting($styles) {
$styles_set = array();
$styles = trim($styles);
if (empty($styles)) {
return $styles_set;
}
$styles = str_replace(array(
"\r\n",
"\r",
), "\n", $styles);
foreach (explode("\n", $styles) as $style) {
$style = trim($style);
if (empty($style)) {
continue;
}
if (!preg_match('@^ *[a-zA-Z0-9]+ *(\\.[a-zA-Z0-9_-]+ *)*\\| *.+ *$@', $style)) {
return FALSE;
}
list($selector, $label) = explode('|', $style);
$classes = explode('.', $selector);
$element = array_shift($classes);
$configured_style = array(
'name' => trim($label),
'element' => trim($element),
);
if (!empty($classes)) {
$configured_style['attributes'] = array(
'class' => implode(' ', array_map('trim', $classes)),
);
}
$styles_set[] = $configured_style;
}
return $styles_set;
}
}