function Core::computeCKEditor5PluginSubsetConfiguration

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

Computes elements subset configuration for CKEditor 5 plugin.

Every CKEditor 5 plugin that implements the elements subset interface must implement this as well, to ensure a smooth upgrade path.

Parameters

string $cke5_plugin_id: The CKEditor 5 plugin whose subset configuration needs to be computed.

\Drupal\filter\FilterFormatInterface $text_format: The text format based on whose restrictions this should be computed.

Return value

array|null NULL if not needed, otherwise a configuration array (which can itself be a subset of the default configuration of this CKEditor 5 plugin: perhaps only some of the configuration values determine the subset).

Overrides CKEditor4To5UpgradePluginInterface::computeCKEditor5PluginSubsetConfiguration

File

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

Class

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

Namespace

Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade

Code

public function computeCKEditor5PluginSubsetConfiguration(string $cke5_plugin_id, FilterFormatInterface $text_format) : ?array {
  switch ($cke5_plugin_id) {
    case 'ckeditor5_heading':
      $restrictions = $text_format->getHtmlRestrictions();
      if ($restrictions === FALSE) {
        // The default is to allow all headings, which makes sense when there
        // are no restrictions.
        // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Heading::DEFAULT_CONFIGURATION
        return NULL;
      }
      // Otherwise, only enable headings that allowed by the restrictions.
      $configuration = [];
      foreach (range(1, 6) as $index) {
        // 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()
        if (array_key_exists("h{$index}", $restrictions['allowed'])) {
          $configuration['enabled_headings'][] = "heading{$index}";
        }
      }
      return $configuration;
    case 'ckeditor5_alignment':
      $alignment_classes_to_types = [
        'text-align-left' => 'left',
        'text-align-right' => 'right',
        'text-align-center' => 'center',
        'text-align-justify' => 'justify',
      ];
      $restrictions = $text_format->getHtmlRestrictions();
      if ($restrictions === FALSE) {
        // The default is to allow all alignments. This makes sense when there
        // are no restrictions.
        // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Alignment::DEFAULT_CONFIGURATION
        return NULL;
      }
      // Otherwise, enable alignment types based on the provided restrictions.
      // I.e. if a tag is found with a text-align-{alignment type} class,
      // activate that alignment type.
      $configuration = [];
      foreach ($restrictions['allowed'] as $tag) {
        $classes = isset($tag['class']) && is_array($tag['class']) ? $tag['class'] : [];
        foreach (array_keys($classes) as $class) {
          if (isset($alignment_classes_to_types[$class])) {
            $configuration['enabled_alignments'][] = $alignment_classes_to_types[$class];
          }
        }
      }
      if (isset($configuration['enabled_alignments'])) {
        $configuration['enabled_alignments'] = array_unique($configuration['enabled_alignments']);
      }
      return $configuration;
    case 'ckeditor5_list':
      $restrictions = $text_format->getHtmlRestrictions();
      if ($restrictions === FALSE) {
        // The default is to allow a reversed list and a start index, which makes sense when there
        // are no restrictions.
        // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\ListPlugin::default_configuration()
        return NULL;
      }
      $configuration = [];
      $configuration['properties']['reversed'] = !empty($restrictions['allowed']['ol']['reversed']);
      $configuration['properties']['startIndex'] = !empty($restrictions['allowed']['ol']['start']);
      return $configuration;
    case 'media_media':
      $restrictions = $text_format->getHtmlRestrictions();
      if ($restrictions === FALSE) {
        // The default is to not allow the user to override the default view mode.
        // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media::defaultConfiguration()
        return NULL;
      }
      $configuration = [];
      // Check if data-view-mode is allowed.
      $configuration['allow_view_mode_override'] = !empty($restrictions['allowed']['drupal-media']['data-view-mode']);
      return $configuration;
    case 'ckeditor5_style':
      // @see mapCKEditor4SettingsToCKEditor5Configuration()
      return NULL;
    default:
      throw new \OutOfBoundsException();
  }
}

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