1 ckeditor5.module ckeditor5_get_config($format)

Editor JS settings callback; Add CKEditor config to the page for a format.

Note that this function uses the term "config" to match that of CKEditor's own terminology. It is not related to Backdrop's configuration system.

See https://ckeditor.com/docs/ckeditor5/latest/installation/getting-started/...

Parameters

object $format: The filter format object for which CKEditor is adding its config.

File

core/modules/ckeditor5/ckeditor5.module, line 938
Provides integration with the CKEditor WYSIWYG editor.

Code

function ckeditor5_get_config($format) {
  // Static cache the configuration per format. This function is called once per
  // filtered text field on the page.
  $ckeditor5_configs = &backdrop_static(__FUNCTION__, array());
  $format_id = $format->name;
  if (isset($ckeditor5_configs[$format_id])) {
    return $ckeditor5_configs[$format_id];
  }

  global $language;

  // Loop through all available plugins and check to see if it has been
  // explicitly enabled. At the same time, associate each plugin with its
  // buttons (if any) so we can check if the plugin should be enabled implicitly
  // based on the toolbar.
  $plugin_info = ckeditor5_plugins();
  $all_buttons = array();
  $plugins = array();
  $pseudo_plugins = array();
  foreach ($plugin_info as $plugin_name => $plugin) {
    // Check if this plugin should be enabled.
    if (isset($plugin['enabled_callback'])) {
      if ($plugin['enabled_callback'] === TRUE || $plugin['enabled_callback']($format, $plugin_name)) {
        $plugins[] = $plugin_name;
        if (!empty($plugin['pseudo_plugin'])) {
          $pseudo_plugins[] = $plugin_name;
        }
        // Enable other plugins this plugin depends on.
        if (isset($plugin['plugin_dependencies'])) {
          // Add the plugin and its dependencies to the list of plugins.
          $plugins = array_merge($plugin['plugin_dependencies'], $plugins);
        }
      }
    }
    // Associate each button with its providing plugin.
    if (isset($plugin['buttons'])) {
      foreach ($plugin['buttons'] as $button_name => &$button) {
        $button['plugin'] = $plugin;
        $button['plugin']['name'] = $plugin_name;
        unset($button['plugin']['buttons']);
      }
      $all_buttons = array_merge($all_buttons, $plugin['buttons']);
    }
  }

  // Record needed plugins based on use in the toolbar.
  $toolbar = array();
  foreach ($format->editor_settings['toolbar'] as $button_name) {
    // Sanity check that the button exists in our installation.
    if (isset($all_buttons[$button_name])) {
      // Add in the button parent's overall plugin and its dependencies.
      if (isset($all_buttons[$button_name]['plugin']['name'])) {
        $plugin_name = $all_buttons[$button_name]['plugin']['name'];
        $plugins[] = $plugin_name;
        if (isset($plugin_info[$plugin_name]['plugin_dependencies'])) {
          $plugins = array_merge($plugin_info[$plugin_name]['plugin_dependencies'], $plugins);
        }
      }
      // Add in plugin dependencies for this specific button.
      if (isset($all_buttons[$button_name]['plugin_dependencies'])) {
        $plugins = array_merge($all_buttons[$button_name]['plugin_dependencies'], $plugins);
      }
    }
    $toolbar[] = $button_name;
  }

  // Remove duplicates from the plugin list (as happens with dependencies).
  $plugins = array_unique($plugins);

  // Initialize reasonable defaults that provide expected basic behavior.
  $config = array(
    'language' => $language->langcode,
    'toolbar' => array(
      'items' => $toolbar,
      'shouldNotGroupWhenFull' => TRUE,
    ),
    // Plugin list is not a "real" CKEditor configuration value. It is converted
    // from an array of strings to variables references in the ckeditor5-dll.js
    // file. Exclude the pseudo-plugins that are configuration-only.
    'pluginList' => array_values(array_diff($plugins, $pseudo_plugins)),
  );

  // Add default settings from plugins.
  foreach ($plugins as $plugin_name) {
    if (isset($plugin_info[$plugin_name]['config'])) {
      $config = backdrop_array_merge_deep($config, $plugin_info[$plugin_name]['config']);
    }
  }

  // Add "allow" and "disallow" for CKEditor GeneralHtmlSupportConfig to apply
  // settings from the Filter module. Prevents data loss for content not handled
  // by editor plugins.
  $config['htmlSupport'] = array();
  list($config['htmlSupport']['allow'], $config['htmlSupport']['disallow']) = _ckeditor5_get_generic_html_settings($format);

  // Create a token for access to dialogs.
  if (in_array('backdropLink.BackdropLink', $plugins)) {
    $link_token = filter_editor_dialog_token($format, 'link');
    $config['backdropLink']['dialogUrl'] = url('editor/dialog/link/' . $format->format, array('query' => array(
      'token' => $link_token,
      'calling_path' => $_GET['q'],
    )));
    $config['backdropLink']['buttonLabel'] = t('Advanced');
  }
  if (in_array('backdropImage.BackdropImage', $plugins)) {
    $image_token = filter_editor_dialog_token($format, 'image');
    $config['backdropImage']['dialogUrl'] = url('editor/dialog/image/' . $format->format, array('query' => array(
      'token' => $image_token,
      'calling_path' => $_GET['q'],
    )));
    $config['backdropImage']['uploadUrl'] = url('ckeditor5/upload/image/' . $format->format, array('query' => array(
      'token' => $image_token,
      'calling_path' => $_GET['q'],
    )));
  }

  // Add the style list if configured.
  if (in_array('style.Style', $plugins)) {
    if (!empty($format->editor_settings['style_list'])) {
      $style_list = $format->editor_settings['style_list'];
      $config['style']['definitions'] = $style_list;
    }
  }

  // Add heading list if configured.
  if (in_array('heading.Heading', $plugins)) {
    $html_restrictions = filter_format_allowed_html($format);
    $heading_list = $format->editor_settings['heading_list'];
    $possible_headings = array(
      'h1' => array(
        'model' => 'heading1',
        'view' => 'h1',
        'title' => t('Heading 1'),
      ),
      'h2' => array(
        'model' => 'heading2',
        'view' => 'h2',
        'title' => t('Heading 2'),
      ),
      'h3' => array(
        'model' => 'heading3',
        'view' => 'h3',
        'title' => t('Heading 3'),
      ),
      'h4' => array(
        'model' => 'heading4',
        'view' => 'h4',
        'title' => t('Heading 4'),
      ),
      'h5' => array(
        'model' => 'heading5',
        'view' => 'h5',
        'title' => t('Heading 5'),
      ),
      'h6' => array(
        'model' => 'heading6',
        'view' => 'h6',
        'title' => t('Heading 6'),
      ),
    );
    foreach ($possible_headings as $tag => $heading_config) {
      // Remove if not enabled in the editor settings.
      if (!in_array($tag, $heading_list)) {
        unset($possible_headings[$tag]);
      }
      // Or remove if the HTML filter does not allow it.
      if (is_array($html_restrictions) && !isset($html_restrictions['allowed'][$tag])) {
        unset($possible_headings[$tag]);
      }
    }
    $config['heading']['options'] = array_values($possible_headings);
    // Add the always required normal paragraph formatting.
    array_unshift($config['heading']['options'], array(
      'model' => 'paragraph',
      'title' => t('Paragraph'),
    ));
  }

  backdrop_alter('ckeditor5_config', $config, $format);

  // Save the configuration into the static variable.
  $ckeditor5_configs[$format_id] = $config;
  return $ckeditor5_configs[$format_id];
}