function Ckeditor5Hooks::editorPresave

Implements hook_ENTITY_TYPE_presave() for editor entities.

Attributes

#[Hook('editor_presave')]

File

core/modules/ckeditor5/src/Hook/Ckeditor5Hooks.php, line 385

Class

Ckeditor5Hooks
Hook implementations for ckeditor5.

Namespace

Drupal\ckeditor5\Hook

Code

public function editorPresave(EditorInterface $editor) : void {
  if ($editor->getEditor() === 'ckeditor5') {
    $settings = $editor->getSettings();
    // @see ckeditor5_post_update_list_type()
    if (array_key_exists('ckeditor5_list', $settings['plugins']) && array_key_exists('ckeditor5_sourceEditing', $settings['plugins'])) {
      $source_edited = HTMLRestrictions::fromString(implode(' ', $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags']));
      $format_restrictions = HTMLRestrictions::fromTextFormat($editor->getFilterFormat());
      // If neither <ol type> or <ul type> are allowed through Source Editing
      // (the only way it could possibly be supported until now), and it is
      // not an unrestricted text format (such as "Full HTML"), then set the
      // new "styles" setting for the List plugin to false.
      $ol_type = HTMLRestrictions::fromString('<ol type>');
      $ul_type = HTMLRestrictions::fromString('<ul type>');
      if (!array_key_exists('styles', $settings['plugins']['ckeditor5_list']['properties'])) {
        $settings['plugins']['ckeditor5_list']['properties']['styles'] = $ol_type->diff($source_edited)
          ->allowsNothing() || $ul_type->diff($source_edited)
          ->allowsNothing() || $format_restrictions->isUnrestricted();
      }
      // Update the Source Editing configuration too.
      $settings['plugins']['ckeditor5_sourceEditing']['allowed_tags'] = $source_edited->diff($ol_type)
        ->diff($ul_type)
        ->toCKEditor5ElementsArray();
    }
    $editor->setSettings($settings);
  }
}

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