You are here

public function Core::mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem in Drupal 10

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 [
        'uploadImage',
      ];

    // @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();
  }
}