6 * Integrate client-side editors with Drupal.
10 * Implementation of hook_menu().
12 function wysiwyg_menu($may_cache) {
16 'path' => 'admin/settings/wysiwyg/profile',
17 'title' => t('Wysiwyg'),
18 'callback' => 'wysiwyg_admin',
19 'description' => t('Configure client-side editor profiles.'),
20 'access' => user_access('administer filters'),
27 * Implementation of hook_help().
29 function wysiwyg_help($section) {
31 case
'admin/settings/wysiwyg/profile':
33 $output = '<p>'.
t('A Wysiwyg profile can be associated to an input format. A Wysiwyg profile defines which client-side editor is loaded, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') .
'</p>';
41 * Callback handler for admin pages; menu callback.
43 function wysiwyg_admin($arg = '', $name = '') {
44 require_once
drupal_get_path('module', 'wysiwyg') .
'/wysiwyg.admin.inc';
45 return _wysiwyg_admin($arg, $name);
49 * Implementation of hook_form_alter().
51 * Before Drupal 7, there is no way to easily identify form fields that are
52 * input format enabled. As a workaround, we assign a form #after_build
53 * processing callback that is executed on all forms after they have been
54 * completely built, so form elements are in their effective order
55 * and position already.
57 * @see wysiwyg_process_form()
59 function wysiwyg_form_alter($form_id, &$form) {
60 $form['#after_build'][] = 'wysiwyg_process_form';
61 // Blog module in Drupal 5 uses a wrong/unusual form element key for the input
63 if ($form_id == 'blog_node_form') {
64 $form['body_filter']['format'] = $form['body_filter']['filter'];
65 unset($form['body_filter']['filter']);
70 * Process a textarea for Wysiwyg Editor.
72 * This way, we can recurse into the form and search for certain, hard-coded
73 * elements that have been added by filter_form(). If an input format selector
74 * or input format guidelines element is found, we assume that the preceding
75 * element is the corresponding textarea and use it's #id for attaching
76 * client-side editors.
78 * @see wysiwyg_elements(), filter_form()
80 function wysiwyg_process_form(&$form) {
81 // Iterate over element children; resetting array keys to access last index.
82 if ($children = array_values(element_children($form))) {
83 foreach ($children as
$index => $item) {
84 $element = &$form[$item];
86 // filter_form() always uses the key 'format'. We need a type-agnostic
87 // match to prevent false positives. Also, there must have been at least
88 // one element on this level.
89 if ($item === 'format' && $index > 0) {
90 // Make sure we either match a input format selector or input format
91 // guidelines (displayed if user has access to one input format only).
92 if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
93 // The element before this element is the target form field.
94 $field = &$form[$children[$index - 1]];
96 // Disable #resizable to avoid resizable behavior to hi-jack the UI,
97 // but load the behavior, so the 'none' editor can attach/detach it.
99 if (!isset($field['#resizable']) || !empty($field['#resizable'])) {
100 // Due to our CSS class parsing, we can add arbitrary parameters
101 // for each input format.
102 $extra_class = ' wysiwyg-resizable-1';
103 $field['#resizable'] = FALSE
;
104 drupal_add_js('misc/textarea.js');
107 // Determine the available input formats. The last child element is a
108 // link to "More information about formatting options". When only one
109 // input format is displayed, we also have to remove formatting
110 // guidelines, stored in the child 'format'.
111 $formats = element_children($element);
113 if (($key = array_search('format', $formats)) !== FALSE
) {
114 unset($formats[$key]);
116 foreach ($formats as
$format) {
117 // Default to 'none' editor (Drupal's default behaviors).
120 // Fetch the profile associated to this input format.
121 $profile = wysiwyg_get_profile($format);
123 $editor = $profile->editor
;
124 $status = (int)wysiwyg_user_get_status($profile);
125 // Check editor theme (and reset it if not/no longer available).
126 $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings
['theme']) ?
$profile->settings
['theme'] : ''));
128 // Add plugin settings (first) for this input format.
129 wysiwyg_add_plugin_settings($profile);
130 // Add profile settings for this input format.
131 wysiwyg_add_editor_settings($profile, $theme);
134 // Use a prefix/suffix for a single input format, or attach to input
135 // format selector radio buttons.
136 if (isset($element['format']['guidelines'])) {
137 $element['format']['guidelines']['#prefix'] = '<div class="wysiwyg wysiwyg-format-'.
$format .
' wysiwyg-editor-'.
$editor .
' wysiwyg-field-'.
$field['#id'] .
' wysiwyg-status-'.
$status .
$extra_class .
'">';
138 $element['format']['guidelines']['#suffix'] = '</div>';
139 // Edge-case: Default format contains no input filters.
140 if (empty($element['format']['guidelines']['#value'])) {
141 $element['format']['guidelines']['#value'] = ' ';
145 if (isset($element[$format]['#attributes']['class'])) {
146 $element[$format]['#attributes']['class'] .
= ' ';
149 $element[$format]['#attributes']['class'] = '';
151 $element[$format]['#attributes']['class'] .
= 'wysiwyg wysiwyg-format-'.
$format .
' wysiwyg-editor-'.
$editor .
' wysiwyg-field-'.
$field['#id'] .
' wysiwyg-status-'.
$status .
$extra_class;
155 // If this element is 'format', do not recurse further.
158 // Recurse into children.
159 wysiwyg_process_form($element);
166 * Determine the profile to use for a given input format id.
168 * This function also performs sanity checks for the configured editor in a
169 * profile to ensure that we do not load a malformed editor.
172 * The internal id of an input format.
177 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
179 function wysiwyg_get_profile($format) {
180 if ($profile = wysiwyg_load_profile($format)) {
181 if (wysiwyg_load_editor($profile)) {
189 * Load an editor library and initialize basic Wysiwyg settings.
192 * A wysiwyg editor profile.
195 * TRUE if the editor has been loaded, FALSE if not.
197 * @see wysiwyg_get_profile()
199 function wysiwyg_load_editor($profile) {
200 static
$settings_added;
201 static
$loaded = array();
203 $name = $profile->editor
;
204 // Library files must be loaded only once.
205 if (!isset($loaded[$name])) {
207 $editor = wysiwyg_get_editor($name);
209 // Determine library files to load.
210 // @todo Allow to configure the library/execMode to use.
211 if (isset($profile->settings
['library']) && isset($editor['libraries'][$profile->settings
['library']])) {
212 $library = $profile->settings
['library'];
213 $files = $editor['libraries'][$library]['files'];
216 // Fallback to the first defined library by default (external libraries can change).
217 $library = key($editor['libraries']);
218 $files = array_shift($editor['libraries']);
219 $files = $files['files'];
221 foreach ($files as
$file => $options) {
222 if (is_array($options)) {
223 $options += array('type' => 'module', 'scope' => 'header', 'defer' => FALSE
, 'cache' => TRUE
, 'preprocess' => TRUE
);
224 drupal_add_js($editor['library path'] .
'/' .
$file, $options['type'], $options['scope'], $options['defer'], $options['cache'], $options['preprocess']);
227 drupal_add_js($editor['library path'] .
'/' .
$options);
230 // If editor defines an additional load callback, invoke it.
231 // @todo Isn't the settings callback sufficient?
232 if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
233 $editor['load callback']($editor, $library);
235 // Load JavaScript integration files for this editor.
237 if (isset($editor['js files'])) {
238 $files = $editor['js files'];
240 foreach ($files as
$file) {
241 drupal_add_js($editor['js path'] .
'/' .
$file);
243 // Load CSS stylesheets for this editor.
245 if (isset($editor['css files'])) {
246 $files = $editor['css files'];
248 foreach ($files as
$file) {
249 drupal_add_css($editor['css path'] .
'/' .
$file);
252 drupal_add_js(array('wysiwyg' => array(
253 'configs' => array($editor['name'] => array()),
254 // @todo Move into profile settings.
255 'showToggle' => isset($profile->settings
['show_toggle']) ?
$profile->settings
['show_toggle'] : TRUE
,
256 // @todo Move into (global) editor settings.
257 // If JS compression is enabled, at least TinyMCE is unable to determine
258 // its own base path and exec mode since it can't find the script name.
259 'editorBasePath' => base_path() .
$editor['library path'],
260 'execMode' => $library,
263 $loaded[$name] = TRUE
;
266 $loaded[$name] = FALSE
;
270 // Add basic Wysiwyg settings if any editor has been added.
271 if (!isset($settings_added) && $loaded[$name]) {
272 drupal_add_js(array('wysiwyg' => array(
273 'configs' => array(),
274 'disable' => t('Disable rich-text'),
275 'enable' => t('Enable rich-text'),
278 // Initialize our namespaces in the *header* to do not force editor
279 // integration scripts to check and define Drupal.wysiwyg on its own.
280 drupal_add_js(wysiwyg_get_path('wysiwyg.init.js'), 'core');
282 // The 'none' editor is a special editor implementation, allowing us to
283 // attach and detach regular Drupal behaviors just like any other editor.
284 drupal_add_js(wysiwyg_get_path('editors/js/none.js'));
286 // Add wysiwyg.js to the footer to ensure it's executed after the
287 // Drupal.settings array has been rendered and populated. Also, since editor
288 // library initialization functions must be loaded first by the browser,
289 // and Drupal.wysiwygInit() must be executed AFTER editors registered
290 // their callbacks and BEFORE Drupal.behaviors are applied, this must come
292 drupal_add_js(wysiwyg_get_path('wysiwyg.js'), 'module', 'footer');
294 $settings_added = TRUE
;
297 return $loaded[$name];
301 * Add editor settings for a given input format.
303 function wysiwyg_add_editor_settings($profile, $theme) {
304 static
$formats = array();
306 if (!isset($formats[$profile->format
])) {
307 $config = wysiwyg_get_editor_config($profile, $theme);
308 // drupal_to_js() does not properly convert numeric array keys, so we need
309 // to use a string instead of the format id.
310 drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor
=> array('format'.
$profile->format
=> $config)))), 'setting');
311 $formats[$profile->format
] = TRUE
;
316 * Add settings for external plugins.
318 * Plugins can be used in multiple profiles, but not necessarily in all. Because
319 * of that, we need to process plugins for each profile, even if most of their
320 * settings are not stored per profile.
322 * Implementations of hook_wysiwyg_plugin() may execute different code for each
323 * editor. Therefore, we have to invoke those implementations for each editor,
324 * but process the resulting plugins separately for each profile.
326 * Drupal plugins differ to native plugins in that they have plugin-specific
327 * definitions and settings, which need to be processed only once. But they are
328 * also passed to the editor to prepare settings specific to the editor.
329 * Therefore, we load and process the Drupal plugins only once, and hand off the
330 * effective definitions for each profile to the editor.
333 * A wysiwyg editor profile.
335 * @todo Rewrite wysiwyg_process_form() to build a registry of effective
336 * profiles in use, so we can process plugins in multiple profiles in one shot
337 * and simplify this entire function.
339 function wysiwyg_add_plugin_settings($profile) {
340 static
$plugins_native = array();
342 $editor = wysiwyg_get_editor($profile->editor
);
343 // Assume that this editor does not support neither native external plugins
344 // nor Drupal plugins if it does not provide a callback.
345 if (!(isset($editor['plugin settings callback']) && function_exists($editor['plugin settings callback']))) {
349 // Load our own plugins.
350 include_once
drupal_get_path('module', 'wysiwyg') .
'/wysiwyg.plugins.inc';
352 // Collect native external plugins for this editor provided via
353 // hook_wysiwyg_plugin().
354 if (!array_key_exists($editor['name'], $plugins_native)) {
355 $plugins_native[$editor['name']] = module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']);
357 // Only keep native external plugins that are enabled in this profile.
358 foreach ($plugins_native[$editor['name']] as
$plugin => $meta) {
359 if (!isset($profile->settings
['buttons'][$plugin])) {
360 unset($plugins_native[$editor['name']][$plugin]);
363 // Invoke the editor's plugin settings callback, so it can populate the
364 // settings for native external plugins with custom, required values.
365 $settings_native = $editor['plugin settings callback']($editor, $profile, $plugins_native[$editor['name']]);
367 drupal_add_js(array('wysiwyg' => array('plugins' => array($profile->editor
=> $settings_native))), 'setting');
371 * Retrieve available themes for an editor.
373 * Editor themes control the visual presentation of an editor.
376 * A wysiwyg editor profile; passed/altered by reference.
377 * @param $selected_theme
378 * An optional theme name that ought to be used.
381 * An array of theme names, or a single, checked theme name if $selected_theme
384 function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL
) {
385 static
$themes = array();
387 if (!isset($themes[$profile->editor
])) {
388 $editor = wysiwyg_get_editor($profile->editor
);
389 if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
390 $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
392 // Fallback to 'default' otherwise.
394 $themes[$editor['name']] = array('default');
398 // Check optional $selected_theme argument, if given.
399 if (isset($selected_theme)) {
400 // If the passed theme name does not exist, use the first available.
401 if (!isset($themes[$profile->editor
][$selected_theme])) {
402 $selected_theme = $profile->settings
['theme'] = $themes[$profile->editor
][0];
406 return isset($selected_theme) ?
$selected_theme : $themes[$profile->editor
];
410 * Return plugin metadata from the plugin registry.
412 * @param $editor_name
413 * The internal name of an editor to return plugins for.
416 * An array for each plugin.
418 function wysiwyg_get_plugins($editor_name) {
420 if (!empty($editor_name)) {
421 $editor = wysiwyg_get_editor($editor_name);
422 // Add internal editor plugins.
423 if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
424 $plugins = $editor['plugin callback']($editor);
426 // Load our own plugins.
427 include_once
drupal_get_path('module', 'wysiwyg') .
'/wysiwyg.plugins.inc';
429 // Add editor plugins provided via hook_wysiwyg_plugin().
430 $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
436 * Return an array of initial editor settings for a Wysiwyg profile.
438 function wysiwyg_get_editor_config($profile, $theme) {
439 $editor = wysiwyg_get_editor($profile->editor
);
441 if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
442 $settings = $editor['settings callback']($editor, $profile->settings
, $theme);
448 * Retrieve stylesheets for HTML/IFRAME-based editors.
450 * This assumes that the content editing area only needs stylesheets defined
451 * for the scope 'theme'.
454 * An array containing CSS files, including proper base path.
456 function wysiwyg_get_css() {
463 foreach (drupal_add_css() as
$media => $css) {
464 if ($media != 'print') {
465 foreach ($css['theme'] as
$filepath => $preprocess) {
466 $files[] = base_path() .
$filepath;
474 * Load all profiles. Just load one profile if $name is passed in.
476 function wysiwyg_load_profile($format = '') {
479 if (!isset($profiles)) {
481 $result = db_query('SELECT * FROM {wysiwyg}');
482 while ($profile = db_fetch_object($result)) {
483 $profile->settings
= unserialize($profile->settings
);
484 $profiles[$profile->format
] = $profile;
488 return ($format && isset($profiles[$format]) ?
$profiles[$format] : ($format ? FALSE
: $profiles));
492 * Implementation of hook_user().
494 function wysiwyg_user($type, &$edit, &$user, $category = NULL
) {
495 if ($type == 'form' && $category == 'account') {
496 // @todo http://drupal.org/node/322433
497 $profile = new stdClass
;
498 if (isset($profile->settings
['user_choose']) && $profile->settings
['user_choose']) {
499 $form['wysiwyg'] = array(
500 '#type' => 'fieldset',
501 '#title' => t('Wysiwyg Editor settings'),
503 '#collapsible' => TRUE
,
504 '#collapsed' => TRUE
,
506 $form['wysiwyg']['wysiwyg_status'] = array(
507 '#type' => 'checkbox',
508 '#title' => t('Enable editor by default'),
509 '#default_value' => isset($user->wysiwyg_status
) ?
$user->wysiwyg_status
: (isset($profile->settings
['default']) ?
$profile->settings
['default'] : FALSE
),
510 '#return_value' => 1,
511 '#description' => t('If enabled, rich-text editing is enabled by default in textarea fields.'),
513 return array('wysiwyg' => $form);
516 elseif ($type == 'validate' && isset($edit['wysiwyg_status'])) {
517 return array('wysiwyg_status' => $edit['wysiwyg_status']);
521 function wysiwyg_user_get_status($profile) {
524 if (!empty($profile->settings
['user_choose']) && isset($user->wysiwyg_status
)) {
525 $status = $user->wysiwyg_status
;
528 $status = isset($profile->settings
['default']) ?
$profile->settings
['default'] : TRUE
;
535 * @defgroup wysiwyg_api Wysiwyg API
538 * @todo Forked from Panels; abstract into a separate API module that allows
539 * contrib modules to define supported include/plugin types.
543 * Return library information for a given editor.
546 * The internal name of an editor.
549 * The library information for the editor, or FALSE if $name is unknown or not
550 * installed properly.
552 function wysiwyg_get_editor($name) {
553 $editors = wysiwyg_get_all_editors();
554 return isset($editors[$name]) && $editors[$name]['installed'] ?
$editors[$name] : FALSE
;
558 * Compile a list holding all supported editors including installed editor version information.
560 function wysiwyg_get_all_editors() {
563 if (isset($editors)) {
567 $editors = wysiwyg_load_includes('editors', 'editor');
568 foreach ($editors as
$editor => $properties) {
569 // Fill in required properties.
570 $editors[$editor] += array(
573 'download url' => '',
574 'editor path' => wysiwyg_get_path($properties['name']),
575 'library path' => wysiwyg_get_path($properties['name']),
576 'libraries' => array(),
577 'version callback' => NULL
,
578 'themes callback' => NULL
,
579 'settings callback' => NULL
,
580 'plugin callback' => NULL
,
581 'plugin settings callback' => NULL
,
582 'versions' => array(),
583 'js path' => $properties['path'] .
'/js',
584 'css path' => $properties['path'] .
'/css',
586 // Check whether library is present.
587 if (!($editors[$editor]['installed'] = file_exists($properties['library path']))) {
590 // Detect library version.
591 if (function_exists($editors[$editor]['version callback'])) {
592 $editors[$editor]['installed version'] = $editors[$editor]['version callback']($properties);
594 if (empty($editors[$editor]['installed version'])) {
595 $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
596 $editors[$editor]['installed'] = FALSE
;
599 // Determine to which supported version the installed version maps.
600 ksort($editors[$editor]['versions']);
602 foreach ($editors[$editor]['versions'] as
$supported_version => $version_properties) {
603 if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
604 $version = $supported_version;
608 $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $properties['title']));
609 $editors[$editor]['installed'] = FALSE
;
612 // Apply library version specific definitions and overrides.
613 $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
614 unset($editors[$editor]['versions']);
620 * Load include files for wysiwyg implemented by all modules.
623 * The type of includes to search for, can be 'editors'.
625 * The hook name to invoke.
627 * An optional include file name without .inc extension to limit the search to.
629 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
631 function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL
) {
632 // Determine implementations.
633 $directories = wysiwyg_get_directories($type);
634 $directories['wysiwyg_'] = wysiwyg_get_path($type);
635 $file_list = array();
636 foreach ($directories as
$module => $path) {
637 $file_list[$module] = drupal_system_listing("$file" .
'.inc$', $path, 'name', 0);
640 // Load implementations.
642 foreach (array_filter($file_list) as
$module => $files) {
643 foreach ($files as
$file) {
644 include_once
'./' .
$file->filename
;
645 $result = _wysiwyg_process_include('wysiwyg', $module .
$file->name
, dirname($file->filename
), $hook);
646 if (is_array($result)) {
647 $info = array_merge($info, $result);
655 * Helper function to build module/file paths.
658 * A file or directory in a module to return.
660 * Whether to prefix the resulting path with base_path().
662 * The module name to use as prefix.
665 * The path to the specified file in a module.
667 function wysiwyg_get_path($file = '', $base_path = FALSE
, $module = 'wysiwyg') {
668 $base_path = ($base_path ?
base_path() : '');
669 return $base_path .
drupal_get_path('module', $module) .
'/' .
$file;
673 * Return a list of directories by modules implementing wysiwyg_include_directory().
676 * The type of a plugin; can be 'editors'.
679 * An array containing module names suffixed with '_' and their defined
682 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
684 function wysiwyg_get_directories($plugintype) {
685 $directories = array();
686 foreach (module_implements('wysiwyg_include_directory') as
$module) {
687 $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
688 if (isset($result) && is_string($result)) {
689 $directories[$module .
'_'] = drupal_get_path('module', $module) .
'/'.
$result;
696 * Process a single hook implementation of a wysiwyg editor.
699 * The module that owns the hook.
701 * Either the module or 'wysiwyg_' . $file->name
703 * The name of the hook being invoked.
705 function _wysiwyg_process_include($module, $identifier, $path, $hook) {
706 $function = $identifier .
'_' .
$hook;
707 if (!function_exists($function)) {
710 $result = $function();
711 if (!isset($result) || !is_array($result)) {
716 foreach ($result as
$editor => $properties) {
717 $result[$editor]['module'] = $module;
718 $result[$editor]['name'] = $editor;
719 $result[$editor]['path'] = $path;
725 * @} End of "defgroup wysiwyg_api".