1 ckeditor.module ckeditor_get_settings($format, $existing_settings)

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

Parameters

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

$existing_settings: Settings that have already been added to the page by filters.

File

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

Code

function ckeditor_get_settings($format, $existing_settings) {
  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 = ckeditor_plugins();
  $external_plugins = array();
  $external_css = array();
  $all_buttons = array();
  $dependencies = 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) && !empty($plugin['path'])) {
        $external_plugins[$plugin_name]['file'] = $plugin['file'];
        $external_plugins[$plugin_name]['path'] = $plugin['path'];
        if (isset($plugin['css'])) {
          $external_css = array_merge($external_css, $plugin['css']);
        }
      }
    }
    // Associate each plugin with its button.
    if (isset($plugin['buttons'])) {
      if (empty($plugin['internal'])) {
        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']);
    }
  }

  // Change the toolbar separators into groups and record needed plugins based
  // on use in the toolbar.
  $toolbar = array();
  foreach ($format->editor_settings['toolbar'] as $row) {
    foreach ($row as $button_group) {
      $checked_group = array();
      // Check that the group configuration is valid.
      $checked_group['name'] = empty($button_group['name']) ? '' : $button_group['name'];
      if (empty($button_group['items'])) {
        continue;
      }
      foreach ($button_group['items'] as $button_name) {
        // Sanity check that the button exists in our installation.
        if (isset($all_buttons[$button_name])) {
          $checked_group['items'][] = $button_name;

          // Keep track of the needed plugin for this button, if any.
          if (isset($all_buttons[$button_name]['plugin']['path'])) {
            $plugin_name = $all_buttons[$button_name]['plugin']['name'];
            $external_plugin = $all_buttons[$button_name]['plugin'];
            $external_plugins[$plugin_name]['file'] = $external_plugin['file'];
            $external_plugins[$plugin_name]['path'] = $external_plugin['path'];
            if (isset($external_plugin['css'])) {
              $external_css = array_merge($external_css, $external_plugin['css']);
            }
          }

          // Add in dependencies.
          if (isset($all_buttons[$button_name]['dependencies'])) {
            $dependencies = array_merge($dependencies, $all_buttons[$button_name]['dependencies']);
          }
        }
      }
      $toolbar[] = $checked_group;
    }
    $toolbar[] = '/';
  }
  // Remove the trailing slash (end row) from the toolbar.
  if ($toolbar) {
    array_pop($toolbar);
  }

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

  // Collect a list of CSS files to be added to the editor instance.
  $css = array(
    backdrop_get_path('module', 'system') . '/css/system.css',
    backdrop_get_path('module', 'ckeditor') . '/css/ckeditor-iframe.css',
  );
  $css = array_merge($css, $external_css, _ckeditor_theme_css());
  backdrop_alter('ckeditor_css', $css, $format);

  // Convert all paths to be relative to root.
  foreach ($css as $key => $css_path) {
    $css[$key] = base_path() . $css_path;
  }

  // Default plugins that should be removed unless requested explicitly.
  $excluded = array(
    'image',
    'tabletools',
    'tableresize',
    'contextmenu',
  );
  $excluded_plugins = array_diff($excluded, $dependencies);
  $extra_plugins = array_unique(array_merge(array_keys($external_plugins), $dependencies));

  // Initialize reasonable defaults that provide expected basic behavior.
  $settings = array(
    'toolbar' => $toolbar,
    'extraPlugins' => implode(',', $extra_plugins),
    'removePlugins' => implode(',', $excluded_plugins),
    'removeButtons' => '',
    // Empty custom config must be a string.
    // See http://docs.ckeditor.com/#!/guide/dev_configuration.
    'customConfig' => '',
    // Empty styles must be an array.
    // See http://docs.ckeditor.com/#!/guide/dev_styles.
    'stylesSet' => $style_list,
    'contentsCss' => array_values($css),
    'pasteFromWordPromptCleanup' => TRUE,
    'entities' => FALSE,
    'indentClasses' => array('indent1', 'indent2', 'indent3'),
    'justifyClasses' => array('text-align-left', 'text-align-center', 'text-align-right', 'text-align-justify'),
    'coreStyles_strike' => array('element' => 'del'),
    'coreStyles_underline' => array('element' => 'span', 'attributes' => array('class' => 'underline')),
    //'format_tags' => implode(';', $format->editor_settings['format_list']),
    'language' => $language->langcode,
    'resize_dir' => 'vertical',
    'disableNativeSpellChecker' => FALSE,
    'versionCheck' => FALSE,
  );

  // Add the allowedContent setting, which ensures CKEditor only allows tags
  // and attributes that are allowed by the text format for this text editor.
  list($settings['allowedContent'], $settings['disallowedContent']) = ckeditor_get_acf_settings($format);

  // These settings are used specifically by Backdrop.
  $settings['backdrop'] = array(
    'externalPlugins' => $external_plugins,
    'format' => $format->format,
  );

  // Create a token for access to dialogs.
  if (isset($external_plugins['backdroplink'])) {
    $link_token = filter_editor_dialog_token($format, 'link');
    $settings['backdrop']['linkDialogUrl'] = url('editor/dialog/link/' . $format->format, array('query' => array('token' => $link_token, 'calling_path' => $_GET['q'])));
  }
  if (isset($external_plugins['backdropimage'])) {
    $image_token = filter_editor_dialog_token($format, 'image');
    $settings['backdrop']['imageDialogUrl'] = url('editor/dialog/image/' . $format->format, array('query' => array('token' => $image_token, 'calling_path' => $_GET['q'])));
    $settings['imageUploadUrl'] = url('ckeditor/upload/image/' . $format->format, array('query' => array('token' => $image_token, 'calling_path' => $_GET['q'])));
    // Use the same extension check as server side for paste uploads in the
    // upload widget.
    $supported_extensions = image_get_supported_extensions();
    $settings['backdrop']['supportedTypesRegexp'] = 'image/(' . implode('|', $supported_extensions) . ')';
  }
  if (isset($external_plugins['backdropimagecaption'])) {
    $settings['backdrop']['captionFilterEnabled'] = !empty($format->filters['filter_image_caption']->status);
    $settings['backdrop']['alignFilterEnabled'] = !empty($format->filters['filter_image_align']->status);
    $settings['backdrop']['imageCaptionPlaceholderText'] = t('Enter caption text here.');
    $settings['image2_captionedClass'] = 'caption caption-img';
    $settings['image2_alignClasses'] = array('align-left', 'align-center', 'align-right');
  }
  backdrop_alter('ckeditor_settings', $settings, $format);

  return $settings;
}