You are here

function ckeditor_editor_presave in Drupal 10

Implements hook_ENTITY_TYPE_presave().

File

core/modules/ckeditor/ckeditor.module, line 266
Provides integration with the CKEditor WYSIWYG editor.

Code

function ckeditor_editor_presave(EditorInterface $editor) {

  // Only try to update editors using CKEditor 4.
  if ($editor
    ->getEditor() !== 'ckeditor') {
    return FALSE;
  }
  $enabled_plugins = _ckeditor_get_enabled_plugins($editor);

  // Only update if the editor has plugin settings for disabled plugins.
  $needs_update = FALSE;
  $settings = $editor
    ->getSettings();

  // Updates are not needed if plugin settings are not defined for the editor.
  if (!isset($settings['plugins'])) {
    return;
  }
  foreach (array_keys($settings['plugins']) as $plugin_id) {
    if (!in_array($plugin_id, $enabled_plugins, TRUE)) {
      unset($settings['plugins'][$plugin_id]);
      $needs_update = TRUE;
    }
  }
  if ($needs_update) {
    $editor
      ->setSettings($settings);
  }
}