You are here

function wysiwyg_load_editor in Wysiwyg 5

Same name and namespace in other branches
  1. 5.2 wysiwyg.module \wysiwyg_load_editor()
  2. 6.2 wysiwyg.module \wysiwyg_load_editor()
  3. 6 wysiwyg.module \wysiwyg_load_editor()
  4. 7.2 wysiwyg.module \wysiwyg_load_editor()

Load an editor library and initialize basic Wysiwyg settings.

Parameters

$profile: A wysiwyg editor profile.

Return value

TRUE if the editor has been loaded, FALSE if not.

See also

wysiwyg_get_profile()

1 call to wysiwyg_load_editor()
wysiwyg_get_profile in ./wysiwyg.module
Determine the profile to use for a given input format id.

File

./wysiwyg.module, line 198
Integrate client-side editors with Drupal.

Code

function wysiwyg_load_editor($profile) {
  static $settings_added;
  static $loaded = array();
  $name = $profile->editor;

  // Library files must be loaded only once.
  if (!isset($loaded[$name])) {

    // Load editor.
    $editor = wysiwyg_get_editor($name);
    if ($editor) {

      // Determine library files to load.
      // @todo Allow to configure the library/execMode to use.
      if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
        $library = $profile->settings['library'];
        $files = $editor['libraries'][$library]['files'];
      }
      else {

        // Fallback to the first defined library by default (external libraries can change).
        $library = key($editor['libraries']);
        $files = array_shift($editor['libraries']);
        $files = $files['files'];
      }
      foreach ($files as $file => $options) {
        if (is_array($options)) {
          $options += array(
            'type' => 'module',
            'scope' => 'header',
            'defer' => FALSE,
            'cache' => TRUE,
            'preprocess' => TRUE,
          );
          drupal_add_js($editor['library path'] . '/' . $file, $options['type'], $options['scope'], $options['defer'], $options['cache'], $options['preprocess']);
        }
        else {
          drupal_add_js($editor['library path'] . '/' . $options);
        }
      }

      // If editor defines an additional load callback, invoke it.
      // @todo Isn't the settings callback sufficient?
      if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
        $editor['load callback']($editor, $library);
      }

      // Load JavaScript integration files for this editor.
      $files = array();
      if (isset($editor['js files'])) {
        $files = $editor['js files'];
      }
      foreach ($files as $file) {
        drupal_add_js($editor['js path'] . '/' . $file);
      }

      // Load CSS stylesheets for this editor.
      $files = array();
      if (isset($editor['css files'])) {
        $files = $editor['css files'];
      }
      foreach ($files as $file) {
        drupal_add_css($editor['css path'] . '/' . $file);
      }
      drupal_add_js(array(
        'wysiwyg' => array(
          'configs' => array(
            $editor['name'] => array(),
          ),
          // @todo Move into profile settings.
          'showToggle' => isset($profile->settings['show_toggle']) ? $profile->settings['show_toggle'] : TRUE,
          // @todo Move into (global) editor settings.
          // If JS compression is enabled, at least TinyMCE is unable to determine
          // its own base path and exec mode since it can't find the script name.
          'editorBasePath' => base_path() . $editor['library path'],
          'execMode' => $library,
        ),
      ), 'setting');
      $loaded[$name] = TRUE;
    }
    else {
      $loaded[$name] = FALSE;
    }
  }

  // Add basic Wysiwyg settings if any editor has been added.
  if (!isset($settings_added) && $loaded[$name]) {
    drupal_add_js(array(
      'wysiwyg' => array(
        'configs' => array(),
        'disable' => t('Disable rich-text'),
        'enable' => t('Enable rich-text'),
      ),
    ), 'setting');

    // Initialize our namespaces in the *header* to do not force editor
    // integration scripts to check and define Drupal.wysiwyg on its own.
    drupal_add_js(wysiwyg_get_path('wysiwyg.init.js'), 'core');

    // The 'none' editor is a special editor implementation, allowing us to
    // attach and detach regular Drupal behaviors just like any other editor.
    drupal_add_js(wysiwyg_get_path('editors/js/none.js'));

    // Add wysiwyg.js to the footer to ensure it's executed after the
    // Drupal.settings array has been rendered and populated. Also, since editor
    // library initialization functions must be loaded first by the browser,
    // and Drupal.wysiwygInit() must be executed AFTER editors registered
    // their callbacks and BEFORE Drupal.behaviors are applied, this must come
    // last.
    drupal_add_js(wysiwyg_get_path('wysiwyg.js'), 'module', 'footer');
    $settings_added = TRUE;
  }
  return $loaded[$name];
}