You are here

function CKEditor::getJSSettings in CKEditor for WYSIWYG Module 8

Implements \Drupal\editor\Plugin\EditorInterface::getJSSettings().

Overrides EditorPluginInterface::getJSSettings

File

lib/Drupal/ckeditor/Plugin/editor/editor/CKEditor.php, line 114
Definition of \Drupal\ckeditor\Plugin\editor\editor\CKEditor.

Class

CKEditor
Defines a CKEditor-based text editor for Drupal.

Namespace

Drupal\ckeditor\Plugin\editor\editor

Code

function getJSSettings(Editor $editor) {
  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();
  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']($editor) && !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 ($editor->settings['toolbar']['buttons'] as $row_number => $row) {
    $button_group = array();
    foreach ($row as $button_name) {
      if ($button_name === '|') {
        $toolbar[] = $button_group;
        $button_group = array();
      }
      else {

        // Sanity check that the button exists in our installation.
        if (isset($all_buttons[$button_name])) {
          $button_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[] = $button_group;
    $toolbar[] = '/';
  }

  // Collect a list of CSS files to be added to the editor instance.
  $css = array(
    drupal_get_path('module', 'ckeditor') . '/css/ckeditor.css',
    drupal_get_path('module', 'ckeditor') . '/css/ckeditor-iframe.css',
  );
  $css = array_merge($css, $external_css, _ckeditor_theme_css());
  drupal_alter('ckeditor_css', $css, $editor, $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',
    //'forcePasteAsPlainText' => TRUE,
    'contentsCss' => array_values($css),
    'pasteFromWordPromptCleanup' => TRUE,
    'indentClasses' => array(
      'indent1',
      'indent2',
      'indent3',
    ),
    'justifyClasses' => array(
      'align-left',
      'align-center',
      'align-right',
      'align-justify',
    ),
    'coreStyles_underline' => array(
      'element' => 'span',
      'attributes' => array(
        'class' => 'underline',
      ),
    ),
    'format_tags' => implode(';', $editor->settings['toolbar']['format_list']),
    'removeDialogTabs' => 'image:Link;image:advanced;link:advanced',
    'language' => isset($language->language) ? $language->language : '',
    'resize_dir' => 'vertical',
  );

  // These settings are used specifically by Drupal.
  $settings['externalPlugins'] = $external_plugins;
  return $settings;
}