You are here

function editor_ckeditor_get_settings in Editor 7

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

Parameters

object $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.

Return value

array An associative array of CKEditor settings. For a complete list of settings see http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-skin.

1 string reference to 'editor_ckeditor_get_settings'
editor_ckeditor_editor_info in modules/editor_ckeditor/editor_ckeditor.editor.inc
Implements hook_editor_info().

File

modules/editor_ckeditor/editor_ckeditor.module, line 279
Adds CKEditor as a supported editor.

Code

function editor_ckeditor_get_settings($format, $existing_settings) {
  global $language;
  editor_format_ensure_additional_properties($format);

  // 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 = editor_ckeditor_plugins();
  $external_plugins = array();
  $external_css = array();
  $all_buttons = 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']);
            }
          }
        }
      }
      $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'];
  }
  $css = array(
    drupal_get_path('module', 'editor_ckeditor') . '/css/ckeditor-iframe.css',
    drupal_get_path('module', 'editor') . '/css/components/align.module.css',
  );

  // Collect a list of CSS files to be added to the editor instance.
  $css = array_merge($css, $external_css, _editor_ckeditor_theme_css());
  drupal_alter('editor_ckeditor_css', $css, $format);

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

  // Initialize reasonable defaults that provide expected basic behavior.
  $settings = array(
    'toolbar' => $toolbar,
    'extraPlugins' => implode(',', array_keys($external_plugins)),
    'removePlugins' => 'image',
    '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,
    'disableNativeSpellChecker' => FALSE,
    'indentClasses' => array(
      'indent1',
      'indent2',
      'indent3',
    ),
    'justifyClasses' => array(
      'text-align-left',
      'text-align-center',
      'text-align-right',
      'text-align-justify',
    ),
    'coreStyles_underline' => array(
      'element' => 'span',
      'attributes' => array(
        'class' => 'underline',
      ),
    ),
    //'format_tags' => implode(';', $format->editor_settings['format_list']),
    'language' => isset($language->language) ? $language->language : '',
    'resize_dir' => 'vertical',
  );

  // Add the language list if configured.
  $language_list = array();
  $config = array(
    'language_list' => 'un',
  );
  if (isset($format->editor_settings['plugins']['language'])) {
    $config = $format->editor_settings['plugins']['language'];
  }
  $predefined_languages = $config['language_list'] === 'all' ? editor_ckeditor_standard_language_list() : editor_ckeditor_united_nations_language_list();

  // Generate the language_list setting as expected by the CKEditor Language
  // plugin, but key the values by the full language name so that we can sort
  // them later on.
  foreach ($predefined_languages as $langcode => $lang) {
    $english_name = $lang[0];
    $direction = empty($lang[2]) ? NULL : $lang[2];
    if ($direction === 'rtl') {
      $language_list[$english_name] = $langcode . ':' . $english_name . ':rtl';
    }
    else {
      $language_list[$english_name] = $langcode . ':' . $english_name;
    }
  }

  // Sort on full language name.
  ksort($language_list);
  $settings['language_list'] = array_values($language_list);

  // 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']) = editor_ckeditor_get_acf_settings($format);

  // These settings are used specifically by Drupal.
  $settings['drupal'] = array(
    'externalPlugins' => $external_plugins,
    'format' => $format->format,
  );
  if (isset($external_plugins['drupallink'])) {
    $settings['drupal']['linkDialogTitleAdd'] = t('Add Link');
    $settings['drupal']['linkDialogTitleEdit'] = t('Edit Link');
    $settings['drupal']['linkDialogUrl'] = url('editor-ckeditor/dialog/link');
  }
  if (isset($external_plugins['drupalimage'])) {
    $settings['drupal']['imageDialogTitleAdd'] = t('Insert Image');
    $settings['drupal']['imageDialogTitleEdit'] = t('Edit Image');
    $settings['drupal']['imageDialogUrl'] = url('editor-ckeditor/dialog/image');
  }
  if (isset($external_plugins['drupalimagecaption'])) {
    $settings['drupal']['captionFilterEnabled'] = !empty($format->filters['editor_caption']->status);
    $settings['drupal']['alignFilterEnabled'] = !empty($format->filters['editor_align']->status);
    $settings['drupal']['imageCaptionPlaceholderText'] = t('Enter caption text here.');
    $settings['image2_captionedClass'] = 'caption caption-img';
    $settings['image2_alignClasses'] = array(
      'align-left',
      'align-center',
      'align-right',
    );
  }
  drupal_alter('editor_ckeditor_settings', $settings, $format);
  return $settings;
}