You are here

function wysiwyg_add_plugin_settings in Wysiwyg 5.2

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

Add settings for external plugins.

Plugins can be used in multiple profiles, but not necessarily in all. Because of that, we need to process plugins for each profile, even if most of their settings are not stored per profile.

Implementations of hook_wysiwyg_plugin() may execute different code for each editor. Therefore, we have to invoke those implementations for each editor, but process the resulting plugins separately for each profile.

Drupal plugins differ to native plugins in that they have plugin-specific definitions and settings, which need to be processed only once. But they are also passed to the editor to prepare settings specific to the editor. Therefore, we load and process the Drupal plugins only once, and hand off the effective definitions for each profile to the editor.

@todo Rewrite wysiwyg_process_form() to build a registry of effective profiles in use, so we can process plugins in multiple profiles in one shot and simplify this entire function.

Parameters

$profile: A wysiwyg editor profile.

1 call to wysiwyg_add_plugin_settings()
wysiwyg_process_form in ./wysiwyg.module
Process a textarea for Wysiwyg Editor.

File

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

Code

function wysiwyg_add_plugin_settings($profile) {
  static $plugins = array();
  static $processed_plugins = array();
  static $processed_formats = array();

  // Each input format must only processed once.
  // @todo ...as long as we do not have multiple profiles per format.
  if (isset($processed_formats[$profile->format])) {
    return;
  }
  $processed_formats[$profile->format] = TRUE;
  $editor = wysiwyg_get_editor($profile->editor);

  // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
  // and Drupal plugins provided via hook_wysiwyg_include_directory().
  if (!array_key_exists($editor['name'], $plugins)) {
    $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
  }

  // Nothing to do, if there are no plugins.
  if (empty($plugins[$editor['name']])) {
    return;
  }

  // Determine name of proxy plugin for Drupal plugins.
  $proxy = isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '';

  // Process native editor plugins.
  if (isset($editor['plugin settings callback'])) {

    // @todo Require PHP 5.1 in 3.x and use array_intersect_key().
    $profile_plugins_native = array();
    foreach ($plugins[$editor['name']] as $plugin => $meta) {

      // Skip Drupal plugins (handled below).
      if ($plugin === $proxy) {
        continue;
      }

      // Only keep native plugins that are enabled in this profile.
      if (isset($profile->settings['buttons'][$plugin])) {
        $profile_plugins_native[$plugin] = $meta;
      }
    }

    // Invoke the editor's plugin settings callback, so it can populate the
    // settings for native external plugins with required values.
    $settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native);
    if ($settings_native) {
      drupal_add_js(array(
        'wysiwyg' => array(
          'plugins' => array(
            'format' . $profile->format => array(
              'native' => $settings_native,
            ),
          ),
        ),
      ), 'setting');
    }
  }

  // Process Drupal plugins.
  if ($proxy && isset($editor['proxy plugin settings callback'])) {
    $profile_plugins_drupal = array();
    foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
      if (isset($profile->settings['buttons'][$proxy][$plugin])) {

        // JavaScript and plugin-specific settings for Drupal plugins must be
        // loaded and processed only once. Plugin information is cached
        // statically to pass it to the editor's proxy plugin settings callback.
        if (!isset($processed_plugins[$proxy][$plugin])) {
          $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;

          // Load the Drupal plugin's JavaScript.
          drupal_add_js($meta['js path'] . '/' . $meta['js file']);

          // Add plugin-specific settings.
          if (isset($meta['settings'])) {
            drupal_add_js(array(
              'wysiwyg' => array(
                'plugins' => array(
                  'drupal' => array(
                    $plugin => $meta['settings'],
                  ),
                ),
              ),
            ), 'setting');
          }
        }
        else {
          $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
        }
      }
    }

    // Invoke the editor's proxy plugin settings callback, so it can populate
    // the settings for Drupal plugins with custom, required values.
    $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);
    if ($settings_drupal) {
      drupal_add_js(array(
        'wysiwyg' => array(
          'plugins' => array(
            'format' . $profile->format => array(
              'drupal' => $settings_drupal,
            ),
          ),
        ),
      ), 'setting');
    }
  }
}