function Core::mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem

Same name and namespace in other branches
  1. 10 core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php \Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade\Core::mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem()
  2. 11.x core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php \Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade\Core::mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem()

Maps a CKEditor 4 button to the CKEditor 5 equivalent, if it exists.

Generated by inspecting all \Drupal\ckeditor\CKEditorPluginButtonsInterface implementations.

Parameters

string $cke4_button: A valid CKEditor 4 button name.

\Drupal\ckeditor5\HTMLRestrictions $text_format_html_restrictions: The restrictions of the text format, if this upgrade plugin needs to inspect the text format's HTML restrictions to make a decision.

Return value

string[]|null The equivalent CKEditor 5 toolbar items, or NULL if no equivalent exists. In either case, the button names must be added to the annotation.

Overrides CKEditor4To5UpgradePluginInterface::mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem

File

core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php, line 78

Class

Core
Provides the CKEditor 4 to 5 upgrade for Drupal core's CKEditor plugins.

Namespace

Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade

Code

public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_button, HTMLRestrictions $text_format_html_restrictions) : ?array {
  static $alignment_mapped;
  switch ($cke4_button) {
    // @see \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalImage
    case 'DrupalImage':
      return [
        'drupalInsertImage',
      ];
    // @see \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalLink
    case 'DrupalLink':
      return [
        'link',
      ];
    case 'DrupalUnlink':
      return NULL;
    // @see \Drupal\ckeditor\Plugin\CKEditorPlugin\Internal
    case 'Bold':
    case 'Italic':
    case 'Underline':
    case 'Superscript':
    case 'Subscript':
    case 'BulletedList':
    case 'NumberedList':
    case 'Outdent':
    case 'Indent':
    case 'Undo':
    case 'Redo':
      return [
        lcfirst($cke4_button),
      ];
    case 'Blockquote':
      return [
        'blockQuote',
      ];
    case 'JustifyLeft':
    case 'JustifyCenter':
    case 'JustifyRight':
    case 'JustifyBlock':
      if (!isset($alignment_mapped)) {
        $alignment_mapped = TRUE;
        return [
          'alignment',
        ];
      }
      return NULL;
    case 'HorizontalRule':
      return [
        'horizontalLine',
      ];
    case 'Format':
      if ($text_format_html_restrictions->isUnrestricted()) {
        // When no restrictions exist, all tags possibly supported by "Format"
        // in CKEditor 4 must be supported.
        return [
          'heading',
          'codeBlock',
        ];
      }
      $allowed_elements = $text_format_html_restrictions->getAllowedElements();
      // Check if <h*> is supported.
      // Merely checking the existence of the array key is sufficient; this
      // plugin does not set or need any additional attributes.
      // @see \Drupal\filter\Plugin\FilterInterface::getHTMLRestrictions()
      $intersect = array_intersect([
        'h2',
        'h3',
        'h4',
        'h5',
        'h6',
      ], array_keys($allowed_elements));
      // Do not return the 'codeBlock' toolbar item, not even when `<pre>` is
      // allowed in the text format. This ensures that SmartDefaultSettings:
      // - first adds the `code` toolbar item (for inline `<code>`)
      // - then adds `codeBlock` toolbar item (for code blocks: `<pre><code>`)
      // @see https://www.drupal.org/project/drupal/issues/3263384#comment-14446315
      return count($intersect) > 0 ? [
        'heading',
      ] : NULL;
    case 'Table':
      return [
        'insertTable',
      ];
    case 'Source':
      return [
        'sourceEditing',
      ];
    case 'Strike':
      return [
        'strikethrough',
      ];
    case 'Cut':
    case 'Copy':
    case 'Paste':
    case 'PasteText':
    case 'PasteFromWord':
    case 'ShowBlocks':
    case 'Maximize':
    case '-':
      // @see https://www.drupal.org/project/ckeditor5/issues/3211049#comment-14167764
      return NULL;
    // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\RemoveFormat
    case 'RemoveFormat':
      return [
        'removeFormat',
      ];
    // @see \Drupal\ckeditor\Plugin\CKEditorPlugin\StylesCombo
    case 'Styles':
      return [
        'style',
      ];
    // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\specialCharacters
    case 'SpecialChar':
      return [
        'specialCharacters',
      ];
    // @see \Drupal\ckeditor\Plugin\CKEditorPlugin\Language
    case 'Language':
      return [
        'textPartLanguage',
      ];
    // @see \Drupal\media_library\Plugin\CKEditorPlugin\DrupalMediaLibrary
    case 'DrupalMediaLibrary':
      return [
        'drupalMedia',
      ];
    default:
      throw new \OutOfBoundsException();
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.