You are here

function wysiwyg_load_editor in Wysiwyg 7.2

Same name and namespace in other branches
  1. 5.2 wysiwyg.module \wysiwyg_load_editor()
  2. 5 wysiwyg.module \wysiwyg_load_editor()
  3. 6.2 wysiwyg.module \wysiwyg_load_editor()
  4. 6 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 358

Code

function wysiwyg_load_editor($profile) {
  static $settings_added;
  static $loaded = array();
  $path = drupal_get_path('module', 'wysiwyg');
  $name = $profile->editor;

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

    // Load editor.
    $editor = wysiwyg_get_editor($name);
    if ($editor) {
      $default_library_options = array(
        'type' => 'file',
        'scope' => 'header',
        'defer' => FALSE,
        'cache' => TRUE,
        'preprocess' => TRUE,
      );

      // Determine library files to load.
      // @todo Allow to configure the library/execMode to use.
      if (isset($profile->preferences['library']) && isset($editor['libraries'][$profile->preferences['library']])) {
        $library = $profile->preferences['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'];
      }

      // Check whether the editor requires an initialization script.
      if (!empty($editor['init callback'])) {
        $init = $editor['init callback']($editor, $library, $profile);
        if (!empty($init)) {

          // Build a file for each of the editors to hold the init scripts.
          // @todo Aggregate all initialization scripts into one file.
          $uri = 'public://js/wysiwyg/wysiwyg_' . $name . '_' . drupal_hash_base64($init) . '.js';
          $init_exists = file_exists($uri);
          if (!$init_exists) {
            $js_path = dirname($uri);
            file_prepare_directory($js_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
          }

          // Attempt to create the file, or fall back to an inline script (which
          // will not work in Ajax calls).
          if (!$init_exists && !file_unmanaged_save_data($init, $uri, FILE_EXISTS_REPLACE)) {
            drupal_add_js($init, array(
              'type' => 'inline',
            ) + $default_library_options);
          }
          else {
            drupal_add_js($uri, $default_library_options);
          }
        }
      }
      foreach ($files as $file => $options) {
        if (is_array($options)) {
          $options += $default_library_options;
          drupal_add_js($editor['library path'] . '/' . $file, $options);
        }
        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);
      }
      $loaded[$name] = TRUE;
    }
    else {
      $loaded[$name] = FALSE;
    }
  }

  // Check if settings were already added on the page that makes an AJAX call.
  if (isset($_POST['ajax_page_state']) && !empty($_POST['ajax_page_state']['js'][$path . '/wysiwyg.js'])) {
    $settings_added = TRUE;
  }

  // Add basic Wysiwyg settings if any editor has been added.
  if (!isset($settings_added) && $loaded[$name]) {
    drupal_add_js(array(
      'wysiwyg' => array(
        'configs' => array(),
        'plugins' => 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($path . '/wysiwyg.init.js', array(
      'group' => JS_LIBRARY,
    ));

    // 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($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($path . '/wysiwyg.js', array(
      'scope' => 'footer',
    ));
    $settings_added = TRUE;
  }
  return $loaded[$name];
}