You are here

function wysiwyg_tinymce_settings in Wysiwyg 7.2

Same name and namespace in other branches
  1. 5.2 editors/tinymce.inc \wysiwyg_tinymce_settings()
  2. 5 editors/tinymce.inc \wysiwyg_tinymce_settings()
  3. 6.2 editors/tinymce.inc \wysiwyg_tinymce_settings()
  4. 6 editors/tinymce.inc \wysiwyg_tinymce_settings()

Return runtime editor settings for a given wysiwyg profile.

Parameters

$editor: A processed hook_editor() array of editor properties.

$config: An array containing wysiwyg editor profile settings.

$theme: The name of a theme/GUI/skin to use.

Return value

A settings array to be populated in Drupal.settings.wysiwyg.configs.{editor}

1 string reference to 'wysiwyg_tinymce_settings'
wysiwyg_tinymce_editor in editors/tinymce.inc
Plugin implementation of hook_editor().

File

editors/tinymce.inc, line 717
Editor integration functions for TinyMCE.

Code

function wysiwyg_tinymce_settings($editor, $config, $theme) {
  $version = $editor['installed version'];
  $settings = array(
    // @todo Add a setting for this.
    'button_tile_map' => TRUE,
    'document_base_url' => base_path(),
    'mode' => 'none',
    'plugins' => array(),
    'theme' => $theme,
    'width' => '100%',
    // Strict loading mode must be enabled; otherwise TinyMCE would use
    // document.write() in IE and Chrome.
    'strict_loading_mode' => TRUE,
    // TinyMCE's URL conversion magic breaks Drupal modules that use a special
    // syntax for paths. This makes 'relative_urls' obsolete.
    'convert_urls' => FALSE,
    // The default entity_encoding ('named') converts too many characters in
    // languages (like Greek). Since Drupal supports Unicode, we only convert
    // HTML control characters and invisible characters. TinyMCE always converts
    // XML default characters '&', '<', '>'.
    'entities' => '160,nbsp,173,shy,8194,ensp,8195,emsp,8201,thinsp,8204,zwnj,8205,zwj,8206,lrm,8207,rlm',
  );
  if (version_compare($version, '4', '<')) {
    $settings += array(
      'theme_advanced_toolbar_location' => 'top',
    );
  }
  else {
    $settings += array(
      'image_title' => TRUE,
    );
  }
  $check_if_set = array(
    'block_formats',
    'apply_source_formatting',
    'autoresize_min_height',
    'autoresize_max_height',
    'convert_fonts_to_spans',
    'font_styles_merge',
    'image_advtab',
    'image_title',
    'indent',
    'language',
    'paste_as_text',
    'paste_auto_cleanup_on_paste',
    'paste_block_drop',
    'paste_convert_middot_lists',
    'paste_convert_headers_to_strong',
    'paste_data_images',
    'paste_max_consecutive_linebreaks',
    'paste_remove_spans',
    'paste_remove_styles',
    'paste_remove_styles_if_webkit',
    'paste_retain_style_properties',
    'paste_strip_class_attributes',
    'paste_text_linebreaktype',
    'paste_text_notifyalways',
    'paste_text_use_dialog',
    'paste_text_sticky',
    'paste_text_sticky_default',
    'preformatted',
    'remove_linebreaks',
    'resize',
    'style_formats_merge',
    'theme_advanced_blockformats',
    'theme_advanced_path',
    'theme_advanced_resize_horizontal',
    'theme_advanced_resizing',
    'theme_advanced_statusbar_location',
    'theme_advanced_toolbar_location',
    'theme_advanced_toolbar_align',
  );
  foreach ($check_if_set as $setting_name) {
    if (isset($config[$setting_name])) {
      $settings[$setting_name] = $config[$setting_name];
    }
  }
  if (!empty($settings['language']) && $settings['language'] == 'en') {
    unset($settings['language']);
  }
  if (isset($config['verify_html'])) {

    // TinyMCE performs a type-agnostic comparison on this particular setting.
    $settings['verify_html'] = (bool) $config['verify_html'];
  }
  if (!empty($config['style_formats'])) {
    $settings['style_formats'] = json_decode($config['style_formats']);
  }
  if (!empty($config['theme_advanced_styles'])) {
    $settings['theme_advanced_styles'] = implode(';', array_filter(explode("\n", str_replace("\r", '', $config['theme_advanced_styles']))));
  }
  if (!empty($config['menu'])) {
    $settings['menu'] = json_decode($config['menu']);
  }
  if (isset($config['css_setting'])) {
    if ($config['css_setting'] == 'theme') {
      $css_files = wysiwyg_get_css(isset($config['css_theme']) ? $config['css_theme'] : '');
      $settings['content_css'] = version_compare($version, '3.5b3', '>=') ? $css_files : implode(',', $css_files);
    }
    elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
      $settings['content_css'] = strtr($config['css_path'], array(
        '%b' => base_path(),
        '%t' => drupal_get_path('theme', variable_get('theme_default', NULL)),
        '%q' => variable_get('css_js_query_string', ''),
      ));
    }
  }

  // Find the enabled buttons and the button row they belong on.
  // Also map the plugin metadata for each button.
  // @todo What follows is a pain; needs a rewrite.
  // $settings['buttons'] are stacked into $settings['theme_advanced_buttons1']
  // later.
  $settings['buttons'] = array();
  if (!empty($config['buttons']) && is_array($config['buttons'])) {

    // Only array keys in $settings['extensions'] matter; added to
    // $settings['plugins'] later.
    $settings['extensions'] = array();

    // $settings['extended_valid_elements'] are just stacked, unique'd later,
    // and transformed into a comma-separated string in
    // wysiwyg_add_editor_settings().
    // @todo Needs a complete plugin API redesign using arrays for
    //   tag => attributes definitions and array_merge_recursive().
    $settings['extended_valid_elements'] = array();
    $plugins = wysiwyg_get_plugins($editor['name']);
    foreach ($config['buttons'] as $plugin => $buttons) {
      foreach ($buttons as $button => $enabled) {

        // Iterate separately over buttons and extensions properties.
        foreach (array(
          'buttons',
          'extensions',
        ) as $type) {

          // Skip unavailable plugins.
          if (!isset($plugins[$plugin][$type][$button])) {
            continue;
          }

          // Add buttons.
          if ($type == 'buttons') {
            if (!empty($plugins[$plugin]['proxy'])) {
              $settings['buttons'][] = 'drupal_' . $button;
            }
            else {
              $settings['buttons'][] = $button;
            }
          }

          // Add external Drupal plugins to the list of extensions.
          if ($type == 'buttons' && !empty($plugins[$plugin]['proxy'])) {
            $settings['extensions'][_wysiwyg_tinymce_plugin_name('add', 'drupal_' . $button)] = 1;
          }
          elseif ($type == 'buttons' && empty($plugins[$plugin]['internal'])) {
            $settings['extensions'][_wysiwyg_tinymce_plugin_name('add', $plugin)] = 1;
          }
          elseif ($type == 'buttons' && !empty($plugins[$plugin]['load'])) {
            $settings['extensions'][$plugin] = 1;
          }
          elseif ($type == 'extensions' && !empty($plugins[$plugin]['load'])) {
            $settings['extensions'][$plugin] = 1;
          }

          // Allow plugins to add valid HTML elements.
          if (!empty($plugins[$plugin]['extended_valid_elements'])) {
            $settings['extended_valid_elements'] = array_merge($settings['extended_valid_elements'], $plugins[$plugin]['extended_valid_elements']);
          }

          // Allow plugins to add or override global configuration settings.
          if (!empty($plugins[$plugin]['options'])) {
            $settings = array_merge($settings, $plugins[$plugin]['options']);
          }
        }
      }
    }

    // Clean-up.
    $settings['extended_valid_elements'] = array_unique($settings['extended_valid_elements']);
    if ($settings['extensions']) {
      $settings['plugins'] = array_keys($settings['extensions']);
    }
    unset($settings['extensions']);
  }
  if (version_compare($version, '4', '>=')) {
    if (isset($settings['buttons'])) {

      // @todo Allow to sort/arrange editor buttons.
      for ($i = 0; $i < count($settings['buttons']); $i++) {
        $settings['toolbar'][] = $settings['buttons'][$i];
      }
    }

    // TinyMCE 3 allowed the callback to be the name of a function. Convert it
    // to a reference to keep compatibility with IMCE Wysiwyg bridge module.
    // Both isset() and is_string() needed, generates a notice if undefined.
    if (isset($settings['file_browser_callback']) && is_string($settings['file_browser_callback'])) {
      $settings['file_browser_callback'] = wysiwyg_wrap_js_callback($settings['file_browser_callback']);
    }
  }

  // Add theme-specific settings.
  switch ($theme) {
    case 'advanced':
      $settings += array(
        'theme_advanced_resizing_use_cookie' => FALSE,
      );
      if (isset($settings['buttons'])) {

        // These rows explicitly need to be set to be empty, otherwise TinyMCE
        // loads its default buttons of the advanced theme for each row.
        $settings += array(
          'theme_advanced_buttons1' => array(),
          'theme_advanced_buttons2' => array(),
          'theme_advanced_buttons3' => array(),
        );

        // @todo Allow to sort/arrange editor buttons.
        for ($i = 0; $i < count($settings['buttons']); $i++) {
          $settings['theme_advanced_buttons1'][] = $settings['buttons'][$i];
        }
      }
      break;
  }
  unset($settings['buttons']);

  // Convert the config values into the form expected by TinyMCE.
  $csv_settings = array(
    'toolbar',
    'plugins',
    'extended_valid_elements',
    'theme_advanced_buttons1',
    'theme_advanced_buttons2',
    'theme_advanced_buttons3',
  );
  foreach ($csv_settings as $key) {
    if (isset($settings[$key]) && is_array($settings[$key])) {
      $settings[$key] = implode(',', $settings[$key]);
    }
  }
  return $settings;
}