You are here

tinymce_extended.inc in TinyMCE 7

Editor integration functions for TinyMCE.

File

includes/tinymce_extended.inc
View source
<?php

/**
 * @file
 * Editor integration functions for TinyMCE.
 */

/**
 * Plugin implementation of hook_editor().
 */
function tinymce_tinymce_extended_editor() {
  $editor['tinymce_extended'] = array(
    'title' => 'TinyMCE (extended integration)',
    'vendor url' => 'http://tinymce.com',
    'download url' => 'http://tinymce.com/download',
    'libraries' => array(
      '' => array(
        'title' => 'Default',
        'files' => array(
          'tinymce.js' => array(
            'preprocess' => FALSE,
          ),
        ),
      ),
    ),
    'editor path' => drupal_get_path('module', 'tinymce') . '/lib/tinymce',
    'library path' => drupal_get_path('module', 'tinymce') . '/lib/tinymce',
    'js path' => drupal_get_path('module', 'tinymce') . '/js',
    'css path' => drupal_get_path('module', 'tinymce') . '/css',
    'version callback' => 'tinymce_wysiwyg_version',
    'settings callback' => 'tinymce_wysiwyg_settings',
    'plugin callback' => 'tinymce_wysiwyg_plugins',
    'plugin settings callback' => 'tinymce_wysiwyg_plugin_settings',
    'proxy plugin' => array(
      'drupal' => array(
        'load' => TRUE,
        'proxy' => TRUE,
      ),
    ),
    'proxy plugin settings callback' => 'tinymce_wysiwyg_proxy_plugin_settings',
    'versions' => array(
      '4.0' => array(
        'js files' => array(
          'tinymce_extended.js',
        ),
      ),
    ),
  );
  return $editor;
}

/**
 * Detect editor version.
 *
 * @param $editor
 *   An array containing editor properties as returned from hook_editor().
 *
 * @return
 *   The installed editor version.
 */
function tinymce_wysiwyg_version($editor) {
  return '4.0';
}

/**
 * Return JavaScript settings that should be passed to the WYSIWYG editor.
 */
function tinymce_wysiwyg_settings($editor, $config, $theme) {

  // Merge in defaults.
  $tinymce_editor_info = tinymce_editor_info();
  $config = (array) $config + $tinymce_editor_info['tinymce']['default settings'];

  // Modify parameters to match Drupal 8 'js settings callback' as closely as
  // possible. WYSIWYG doesn't pass us the format information.
  $editor = (object) $editor;
  $editor->settings = $config;
  $format = (object) array();
  $existing_settings = array();
  return tinymce_add_settings($editor, $format, $existing_settings);
}

/**
 * Build a JS settings array of native external plugins that need to be loaded separately.
 */
function tinymce_wysiwyg_plugin_settings($editor, $profile, $plugins) {
  $settings = array();

  // Pull the list of required plugins from the TinyMCE JS settings.
  // TODO: Abstract the list of enabled plugins so we can get at this
  // information more easily?
  $editor_js_settings = tinymce_wysiwyg_settings($editor, $profile->settings, NULL);
  $plugins = explode(',', $editor_js_settings['extraPlugins']);
  $plugin_info = tinymce_plugins();
  foreach ($plugins as $name) {

    // Skip plugins that are no longer available.
    if (!isset($plugin_info[$name])) {
      continue;
    }
    $plugin = $plugin_info[$name];

    // Register all plugins that need to be loaded.
    $settings[$name] = array();

    // Add path for native external plugins.
    if (empty($plugin['internal']) && isset($plugin['path'])) {
      $settings[$name]['path'] = base_path() . $plugin['path'] . '/';
    }

    // TinyMCE defaults to 'plugin.js' on its own when filename is not set.
    if (!empty($plugin['file'])) {
      $settings[$name]['fileName'] = $plugin['file'];
    }
  }
  return $settings;
}

/**
 * Build a JS settings array for Drupal plugins loaded via the proxy plugin.
 */
function tinymce_wysiwyg_proxy_plugin_settings($editor, $profile, $plugins) {
  $settings = array();
  foreach ($plugins as $name => $plugin) {

    // Populate required plugin settings.
    $settings[$name] = $plugin['dialog settings'] + array(
      'title' => $plugin['title'],
      'icon' => base_path() . $plugin['icon path'] . '/' . $plugin['icon file'],
      'iconTitle' => $plugin['icon title'],
      // @todo These should only be set if the plugin defined them.
      'css' => base_path() . $plugin['css path'] . '/' . $plugin['css file'],
    );
  }
  return $settings;
}

/**
 * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
 */
function tinymce_wysiwyg_plugins($editor) {

  // Convert the more extensive TinyMCE plugin definitions to WYSIWYG API's
  // simpler form.
  $tinymce_plugins = tinymce_plugins();
  $plugins = array();
  foreach ($tinymce_plugins as $plugin_name => $plugin) {
    if (isset($plugin['buttons'])) {
      foreach ($plugin['buttons'] as $button_name => $button) {
        $plugin['buttons'][$button_name] = $button['label'];
      }
    }
    $plugins[$plugin_name] = $plugin;
  }
  return $plugins;
}

Functions

Namesort descending Description
tinymce_tinymce_extended_editor Plugin implementation of hook_editor().
tinymce_wysiwyg_plugins Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
tinymce_wysiwyg_plugin_settings Build a JS settings array of native external plugins that need to be loaded separately.
tinymce_wysiwyg_proxy_plugin_settings Build a JS settings array for Drupal plugins loaded via the proxy plugin.
tinymce_wysiwyg_settings Return JavaScript settings that should be passed to the WYSIWYG editor.
tinymce_wysiwyg_version Detect editor version.