You are here

class DrupalSpecialChars in CKEditor Special Characters 8

Defines the "specialChars" plugin.

Plugin annotation


@CKEditorPlugin(
  id = "specialchars",
  label = @Translation("Special characters"),
  module = "ckeditor"
)

Hierarchy

Expanded class hierarchy of DrupalSpecialChars

File

src/Plugin/CKEditorPlugin/DrupalSpecialChars.php, line 20

Namespace

Drupal\ckeditor_specialchars\Plugin\CKEditorPlugin
View source
class DrupalSpecialChars extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginContextualInterface {

  /**
   * Default characters to use.
   *
   * Copied from CKEDITOR.config.specialChars in
   * file core/assets/vendor/ckeditor/plugins/specialchar/plugin.js.
   *
   * @var array
   */
  public $defaultCharacters = [
    '!',
    '"',
    '#',
    '$',
    '%',
    '&',
    "'",
    '(',
    ')',
    '*',
    '+',
    '-',
    '.',
    '/',
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    ':',
    ';',
    '<',
    '=',
    '>',
    '?',
    '@',
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
    '[',
    ']',
    '^',
    '_',
    '`',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'q',
    'r',
    's',
    't',
    'u',
    'v',
    'w',
    'x',
    'y',
    'z',
    '{',
    '|',
    '}',
    '~',
    '€',
    '‘',
    '’',
    '“',
    '”',
    '–',
    '—',
    '¡',
    '¢',
    '£',
    '¤',
    '¥',
    '¦',
    '§',
    '¨',
    '©',
    'ª',
    '«',
    '¬',
    '®',
    '¯',
    '°',
    '²',
    '³',
    '´',
    'µ',
    '¶',
    '·',
    '¸',
    '¹',
    'º',
    '»',
    '¼',
    '½',
    '¾',
    '¿',
    'À',
    'Á',
    'Â',
    'Ã',
    'Ä',
    'Å',
    'Æ',
    'Ç',
    'È',
    'É',
    'Ê',
    'Ë',
    'Ì',
    'Í',
    'Î',
    'Ï',
    'Ð',
    'Ñ',
    'Ò',
    'Ó',
    'Ô',
    'Õ',
    'Ö',
    '×',
    'Ø',
    'Ù',
    'Ú',
    'Û',
    'Ü',
    'Ý',
    'Þ',
    'ß',
    'à',
    'á',
    'â',
    'ã',
    'ä',
    'å',
    'æ',
    'ç',
    'è',
    'é',
    'ê',
    'ë',
    'ì',
    'í',
    'î',
    'ï',
    'ð',
    'ñ',
    'ò',
    'ó',
    'ô',
    'õ',
    'ö',
    '÷',
    'ø',
    'ù',
    'ú',
    'û',
    'ü',
    'ý',
    'þ',
    'ÿ',
    'Œ',
    'œ',
    'Ŵ',
    '&#374',
    '&#373',
    'ŷ',
    '‚',
    '‛',
    '„',
    '…',
    '™',
    '►',
    '•',
    '→',
    '⇒',
    '⇔',
    '♦',
    '≈',
  ];

  /**
   * {@inheritdoc}
   */
  public function isInternal() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled(Editor $editor) {
    $settings = $editor
      ->getSettings();
    foreach ($settings['toolbar']['rows'] as $row) {
      foreach ($row as $group) {
        foreach ($group['items'] as $button) {
          if ($button === 'SpecialChar') {
            return TRUE;
          }
        }
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getFile() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {

    // Defaults.
    $config = [
      'characters' => '',
      'replace' => FALSE,
    ];
    $settings = $editor
      ->getSettings();
    if (isset($settings['plugins']['specialchars'])) {
      $config = $settings['plugins']['specialchars'];
    }
    $characters = explode("\n", $config['characters']);
    $characters = array_map('trim', $characters);
    $characters = array_filter($characters, 'strlen');

    // Not replace = append.
    if (!$config['replace']) {
      $characters = array_merge($this->defaultCharacters, $characters);
    }
    return [
      'specialChars' => $characters,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getButtons() {
    return [
      'SpecialChar' => [
        'label' => $this
          ->t('Character map'),
        'image_alternative' => $this
          ->buttonTemplate('special char'),
        'image_alternative_rtl' => $this
          ->buttonTemplate('special char', 'rtl'),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {

    // Defaults.
    $config = [
      'characters' => '',
      'replace' => FALSE,
    ];
    $settings = $editor
      ->getSettings();
    if (isset($settings['plugins']['specialchars'])) {
      $config = $settings['plugins']['specialchars'];
    }
    $form['characters'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Special characters'),
      '#description' => $this
        ->t('One per line'),
      '#default_value' => $config['characters'],
    ];
    $form['replace'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Replace default characters?'),
      '#description' => $this
        ->t('Leaving un-checked will append to default character list.'),
      '#default_value' => $config['replace'],
    ];
    return $form;
  }

  /**
   * CKEditor button template.
   *
   * @param string $name
   *   Button name.
   * @param string $direction
   *   Language direction.
   *
   * @return array
   *   Renderable array.
   *
   * @see \Drupal\ckeditor\Plugin\CKEditorPlugin\Internal::getButtons()
   */
  protected function buttonTemplate($name, $direction = 'ltr') {

    // In the markup below, we mostly use the name (which may include spaces),
    // but in one spot we use it as a CSS class, so strip spaces.
    // Note: this uses str_replace() instead of Html::cleanCssIdentifier()
    // because we must provide these class names exactly how CKEditor expects
    // them in its library, which cleanCssIdentifier() does not do.
    $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,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CKEditorPluginBase::getDependencies public function Returns a list of plugins this plugin requires. Overrides CKEditorPluginInterface::getDependencies 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DrupalSpecialChars::$defaultCharacters public property Default characters to use.
DrupalSpecialChars::buttonTemplate protected function CKEditor button template.
DrupalSpecialChars::getButtons public function Returns the buttons that this plugin provides, along with metadata. Overrides CKEditorPluginButtonsInterface::getButtons
DrupalSpecialChars::getConfig public function Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface::getConfig
DrupalSpecialChars::getFile public function Returns the Drupal root-relative file path to the plugin JavaScript file. Overrides CKEditorPluginInterface::getFile
DrupalSpecialChars::getLibraries public function Returns a list of libraries this plugin requires. Overrides CKEditorPluginBase::getLibraries
DrupalSpecialChars::isEnabled public function Checks if this plugin should be enabled based on the editor configuration. Overrides CKEditorPluginContextualInterface::isEnabled
DrupalSpecialChars::isInternal public function Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginBase::isInternal
DrupalSpecialChars::settingsForm public function Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface::settingsForm
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.