You are here

function wysiwyg_add_plugin_settings in Wysiwyg 6

Same name and namespace in other branches
  1. 5.2 wysiwyg.module \wysiwyg_add_plugin_settings()
  2. 5 wysiwyg.module \wysiwyg_add_plugin_settings()
  3. 6.2 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 336
Integrate client-side editors with Drupal.

Code

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

  // Each input format must only processed once.
  if (isset($processed_formats[$profile->format])) {
    return;
  }
  $editor = wysiwyg_get_editor($profile->editor);

  // Assume that this editor does not support neither native external plugins
  // nor Drupal plugins if it does not provide a callback.
  if (!(isset($editor['plugin settings callback']) && function_exists($editor['plugin settings callback']))) {
    return;
  }

  // Load our own plugins.
  include_once drupal_get_path('module', 'wysiwyg') . '/wysiwyg.plugins.inc';

  // Collect native external plugins for this editor provided via
  // hook_wysiwyg_plugin().
  if (!array_key_exists($editor['name'], $plugins_native)) {
    $plugins_native[$editor['name']] = module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']);
  }

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

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