6 * Integrate client-side editors with Drupal.
10 * Implementation of hook_menu().
12 function wysiwyg_menu() {
14 $items['admin/settings/wysiwyg/profile'] = array(
16 'page callback' => 'wysiwyg_admin',
17 'description' => 'Configure client-side editor profiles.',
18 'access arguments' => array('administer site configuration'),
19 'file' => 'wysiwyg.admin.inc',
25 * Implementation of hook_theme().
27 function wysiwyg_theme() {
29 'wysiwyg_profile_overview' => array('arguments' => array('form' => NULL
)),
30 'wysiwyg_admin_button_table' => array('arguments' => array('form' => NULL
)),
35 * Implementation of hook_help().
37 function wysiwyg_help($path, $arg) {
39 case
'admin/settings/wysiwyg/profile':
41 $output = '<p>'.
t('A Wysiwyg profile can be associated to an input format. A Wysiwyg profile defines, which client-side editor is loaded, can define what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') .
'</p>';
49 * Implementation of hook_elements().
51 * Before Drupal 7, there is no way to easily identify form fields that are
52 * input format enabled. This is 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 that form elements already are in their effective order
57 * @see wysiwyg_process_form()
59 * @todo Remove #wysiwyg_style; the GUI for an editor should be solely handled
60 * via profiles, when profiles are attached to an input format. It makes no
61 * sense to display TinyMCE's simple GUI/theme for the user signature, when
62 * the input format allows users to use advanced HTML and hence, editor
63 * plugins. Fix this here, in wysiwyg_process_element(), and lastly
64 * in wysiwyg_get_editor_config().
66 function wysiwyg_elements() {
68 // @todo Derive editor theme from input format.
69 $type['textarea'] = array('#wysiwyg_style' => 'advanced');
70 $type['form'] = array('#after_build' => array('wysiwyg_process_form'));
75 * Implementation of hook_form_alter().
77 function wysiwyg_form_alter(&$form, &$form_state) {
78 // Disable 'teaser' textarea.
79 if (isset($form['body_field'])) {
80 unset($form['body_field']['teaser_js']);
81 $form['body_field']['teaser_include'] = array();
86 * Process a textarea for Wysiwyg Editor.
88 * This way, we can recurse into the form and search for certain, hard-coded
89 * elements that have been added by filter_form(). If an input format selector
90 * or input format guidelines element is found, we assume that the preceding
91 * element is the corresponding textarea and use it's #id for attaching
92 * client-side editors.
94 * @see wysiwyg_elements(), filter_form()
96 function wysiwyg_process_form(&$form) {
97 // Iterate over element children; resetting array keys to access last index.
98 if ($children = array_values(element_children($form))) {
99 foreach ($children as
$index => $item) {
100 $element = &$form[$item];
102 // filter_form() always uses the key 'format'. We need a type-agnostic
103 // match to prevent false positives. Also, there must have been at least
104 // one element on this level.
105 if ($item === 'format' && $index > 0) {
106 // Make sure we either match a input format selector or input format
107 // guidelines (displayed if user has access to one input format only).
108 if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
109 // The element before this element is the target form field.
110 $field = &$form[$children[$index - 1]];
112 // Disable #resizable to avoid resizable behavior to hi-jack the UI,
113 // but load the behavior, so the 'none' editor can attach/detach it.
115 if (!empty($field['#resizable'])) {
116 // Due to our CSS class parsing, we can add arbitrary parameters
117 // for each input format.
118 $extra_class = ' wysiwyg-resizable-1';
119 $field['#resizable'] = FALSE
;
120 drupal_add_js('misc/textarea.js');
123 // Determine the available input formats. The last child element is a
124 // link to "More information about formatting options". When only one
125 // input format is displayed, we also have to remove formatting
126 // guidelines, stored in the child 'format'.
127 $formats = element_children($element);
129 unset($formats['format']);
130 foreach ($formats as
$format) {
131 // Default to 'none' editor (Drupal's default behaviors).
134 // Fetch the profile associated to this input format.
135 $profile = wysiwyg_get_profile($format);
137 $editor = $profile->editor
;
138 // Check editor theme (and reset it if not/no longer available).
139 $theme = wysiwyg_get_editor_themes($profile, $field['#wysiwyg_style']);
141 // Add profile settings for this input format.
142 wysiwyg_add_editor_settings($profile, $theme);
143 // Add plugin settings for this input format.
144 wysiwyg_add_plugin_settings($profile);
146 $theme = ' wysiwyg-theme-'.
$theme;
149 // Use a prefix/suffix for a single input format, or attach to input
150 // format selector radio buttons.
151 if (isset($element['format']['guidelines'])) {
152 $element[$format]['#prefix'] = '<div class="wysiwyg wysiwyg-editor-'.
$editor .
' wysiwyg-field-'.
$field['#id'] .
$theme .
$extra_class .
'">';
153 $element[$format]['#suffix'] = '</div>';
156 if (isset($element[$format]['#attributes']['class'])) {
157 $element[$format]['#attributes']['class'] .
= ' ';
160 $element[$format]['#attributes']['class'] = '';
162 $element[$format]['#attributes']['class'] .
= 'wysiwyg wysiwyg-editor-'.
$editor .
' wysiwyg-field-'.
$field['#id'] .
$theme .
$extra_class;
166 // If this element is 'format', do not recurse further.
169 // Recurse into children.
170 wysiwyg_process_form($element);
177 * Determine the profile to use for a given input format id.
179 * This function also performs sanity checks for the configured editor in a
180 * profile to ensure that we do not load a malformed editor.
183 * The internal id of an input format.
188 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
190 function wysiwyg_get_profile($format) {
191 if ($profile = wysiwyg_load_profile($format)) {
192 if (wysiwyg_load_editor($profile)) {
200 * Load an editor library and initialize basic Wysiwyg settings.
203 * A wysiwyg editor profile.
206 * TRUE if the editor has been loaded, FALSE if not.
208 * @see wysiwyg_get_profile()
210 function wysiwyg_load_editor($profile) {
211 static
$settings_added;
212 static
$loaded = array();
214 $name = $profile->editor
;
215 // Library files must be loaded only once.
216 if (!isset($loaded[$name])) {
218 $editor = wysiwyg_get_editor($name);
220 // Determine library files to load.
221 // @todo Allow to configure the library/execMode to use.
222 if (isset($profile->settings
['library']) && isset($editor['libraries'][$profile->settings
['library']])) {
223 $library = $profile->settings
['library'];
224 $files = $editor['libraries'][$profile->settings
['library']]['files'];
227 // Fallback to the first by default (external libraries can change).
228 $library = key($editor['libraries']);
229 $files = array_shift($editor['libraries']);
230 $files = $files['files'];
232 foreach ($files as
$file) {
233 drupal_add_js($editor['library path'] .
'/' .
$file);
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, 'module', 'screen');
252 $status = wysiwyg_user_get_status($profile);
253 drupal_add_js(array('wysiwyg' => array(
254 'configs' => array($editor['name'] => array()),
255 // @todo Move into profile settings.
256 'showToggle' => isset($profile->settings
['show_toggle']) ?
$profile->settings
['show_toggle'] : TRUE
,
257 // @todo http://drupal.org/node/322433
259 // @todo Move into (global) editor settings.
260 // If JS compression is enabled, at least TinyMCE is unable to determine
261 // its own base path and exec mode since it can't find the script name.
262 'editorBasePath' => base_path() .
$editor['library path'],
263 'execMode' => $library,
266 $loaded[$name] = TRUE
;
269 $loaded[$name] = FALSE
;
273 // Add basic Wysiwyg settings if any editor has been added.
274 if (!isset($settings_added) && $loaded[$name]) {
275 drupal_add_js(array('wysiwyg' => array(
276 'configs' => array(),
277 'disable' => t('Disable rich-text'),
278 'enable' => t('Enable rich-text'),
281 // Initialize our namespaces in the *header* to do not force editor
282 // integration scripts to check and define Drupal.wysiwyg on its own.
283 drupal_add_js(wysiwyg_get_path('wysiwyg.init.js'), 'core');
285 // The 'none' editor is a special editor implementation, allowing us to
286 // attach and detach regular Drupal behaviors just like any other editor.
287 drupal_add_js(wysiwyg_get_path('editors/js/none.js'));
289 // Add wysiwyg.js to the footer to ensure it's executed after the
290 // Drupal.settings array has been rendered and populated. Also, since editor
291 // library initialization functions must be loaded first by the browser,
292 // and Drupal.wysiwygInit() must be executed AFTER editors registered
293 // their callbacks and BEFORE Drupal.behaviors are applied, this must come
295 drupal_add_js(wysiwyg_get_path('wysiwyg.js'), 'module', 'footer');
297 $settings_added = TRUE
;
300 return $loaded[$name];
306 function wysiwyg_add_editor_settings($profile, $theme) {
307 static
$editors = array();
309 if (!isset($editors[$profile->editor
][$theme])) {
310 $config = wysiwyg_get_editor_config($profile, $theme);
311 drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor
=> array($theme => $config)))), 'setting');
312 $editors[$profile->editor
][$theme] = TRUE
;
317 * Add settings for external plugins.
320 * A wysiwyg editor profile.
322 function wysiwyg_add_plugin_settings($profile) {
323 static
$plugins_added = array();
325 if (!isset($plugins_added[$profile->editor
])) {
327 $editor = wysiwyg_get_editor($profile->editor
);
328 // Collect editor plugins provided via hook_wysiwyg_plugin().
329 $info = module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']);
330 // Only keep enabled plugins in this profile.
331 foreach ($info as
$plugin => $meta) {
332 if (!isset($profile->settings
['buttons'][$plugin])) {
333 unset($info[$plugin]);
337 if (isset($editor['plugin settings callback']) && function_exists($editor['plugin settings callback'])) {
338 $plugins = $editor['plugin settings callback']($editor, $profile, $info);
341 drupal_add_js(array('wysiwyg' => array('plugins' => array($profile->editor
=> $plugins))), 'setting');
343 $plugins_added[$profile->editor
] = TRUE
;
348 * Grab the themes available to Wysiwyg Editor.
350 * Wysiwyg Editor themes control the functionality and buttons that are available to a
351 * user. Themes are only looked for within the default Wysiwyg Editor theme directory.
354 * A wysiwyg editor profile; passed/altered by reference.
355 * @param $selected_theme
356 * An optional theme name that ought to be used.
359 * An array of theme names, or a single, checked theme name if $selected_theme
362 function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL
) {
363 static
$themes = array();
365 if (!isset($themes[$profile->editor
])) {
366 $editor = wysiwyg_get_editor($profile->editor
);
367 if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
368 $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
370 // Fallback to 'default' otherwise.
372 $themes[$editor['name']] = array('default');
376 // Check optional $selected_theme argument, if given.
377 if (isset($selected_theme)) {
378 // If the passed theme name does not exist, use the first available.
379 if (!isset($themes[$profile->editor
][$selected_theme])) {
380 $selected_theme = $profile->settings
['theme'] = $themes[$profile->editor
][0];
384 return isset($selected_theme) ?
$selected_theme : $themes[$profile->editor
];
388 * Return plugin metadata from the plugin registry.
390 * @param $editor_name
391 * The internal name of an editor to return plugins for.
394 * An array for each plugin.
396 function wysiwyg_get_plugins($editor_name) {
398 if (!empty($editor_name)) {
399 $editor = wysiwyg_get_editor($editor_name);
400 // Add internal editor plugins.
401 if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
402 $plugins = $editor['plugin callback']($editor);
404 // Load our own plugins.
405 include_once
drupal_get_path('module', 'wysiwyg') .
'/wysiwyg.plugins.inc';
407 // Add editor plugins provided via hook_wysiwyg_plugin().
408 $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
414 * Return an array of initial Wysiwyg Editor config options from the current role.
416 function wysiwyg_get_editor_config($profile, $theme) {
417 $editor = wysiwyg_get_editor($profile->editor
);
419 if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
420 $settings = $editor['settings callback']($editor, $profile->settings
, $theme);
426 * Load all profiles. Just load one profile if $name is passed in.
428 function wysiwyg_load_profile($format = '') {
431 if (!isset($profiles)) {
433 $result = db_query('SELECT * FROM {wysiwyg}');
434 while ($profile = db_fetch_object($result)) {
435 $profile->settings
= unserialize($profile->settings
);
436 $profiles[$profile->format
] = $profile;
440 return ($format && isset($profiles[$format]) ?
$profiles[$format] : ($format ? FALSE
: $profiles));
444 * Implementation of hook_user().
446 function wysiwyg_user($type, &$edit, &$user, $category = NULL
) {
447 if ($type == 'form' && $category == 'account') {
448 // @todo http://drupal.org/node/322433
449 $profile = new stdClass
;
450 if (isset($profile->settings
['user_choose']) && $profile->settings
['user_choose']) {
451 $form['wysiwyg'] = array(
452 '#type' => 'fieldset',
453 '#title' => t('Wysiwyg Editor settings'),
455 '#collapsible' => TRUE
,
456 '#collapsed' => TRUE
,
458 $form['wysiwyg']['wysiwyg_status'] = array(
459 '#type' => 'checkbox',
460 '#title' => t('Enable editor by default'),
461 '#default_value' => isset($user->wysiwyg_status
) ?
$user->wysiwyg_status
: (isset($profile->settings
['default']) ?
$profile->settings
['default'] : FALSE
),
462 '#return_value' => 1,
463 '#description' => t('If enabled, rich-text editing is enabled by default in textarea fields.'),
465 return array('wysiwyg' => $form);
468 elseif ($type == 'validate' && isset($edit['wysiwyg_status'])) {
469 return array('wysiwyg_status' => $edit['wysiwyg_status']);
473 function wysiwyg_user_get_status($profile) {
476 if ($profile->settings
['user_choose'] && isset($user->wysiwyg_status
)) {
477 $status = $user->wysiwyg_status
;
480 $status = isset($profile->settings
['default']) ?
$profile->settings
['default'] : TRUE
;
487 * @defgroup wysiwyg_api Wysiwyg API
490 * @todo Forked from Panels; abstract into a separate API module that allows
491 * contrib modules to define supported include/plugin types.
495 * Return library information for a given editor.
498 * The internal name of an editor.
501 * The library information for the editor, or FALSE if $name is unknown or not
502 * installed properly.
504 function wysiwyg_get_editor($name) {
505 $editors = wysiwyg_get_all_editors();
506 return isset($editors[$name]) && $editors[$name]['installed'] ?
$editors[$name] : FALSE
;
510 * Compile a list holding all supported editors including installed editor version information.
512 function wysiwyg_get_all_editors() {
515 if (isset($editors)) {
519 $editors = wysiwyg_load_includes('editors', 'editor');
520 foreach ($editors as
$editor => $properties) {
521 // Fill in required properties.
522 $editors[$editor] += array(
525 'download url' => '',
526 'editor path' => wysiwyg_get_path($properties['name']),
527 'library path' => wysiwyg_get_path($properties['name']),
528 'libraries' => array(),
529 'version callback' => NULL
,
530 'themes callback' => NULL
,
531 'settings callback' => NULL
,
532 'plugin callback' => NULL
,
533 'plugin settings callback' => NULL
,
534 'versions' => array(),
535 'js path' => $properties['path'] .
'/js',
536 'css path' => $properties['path'] .
'/css',
538 // Check whether library is present.
539 if (!($editors[$editor]['installed'] = file_exists($properties['library path']))) {
542 // Detect library version.
543 if (function_exists($editors[$editor]['version callback'])) {
544 $editors[$editor]['installed version'] = $editors[$editor]['version callback']($properties);
546 if (empty($editors[$editor]['installed version'])) {
547 $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
548 $editors[$editor]['installed'] = FALSE
;
551 // Determine to which supported version the installed version maps.
552 ksort($editors[$editor]['versions']);
554 foreach ($editors[$editor]['versions'] as
$supported_version => $version_properties) {
555 if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
556 $version = $supported_version;
560 $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $properties['title']));
561 $editors[$editor]['installed'] = FALSE
;
564 // Apply library version specific definitions and overrides.
565 $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
566 unset($editors[$editor]['versions']);
567 $editors[$editor]['title'] = $editors[$editor]['title'] .
' ' .
$editors[$editor]['installed version'];
573 * Load include files for wysiwyg implemented by all modules.
576 * The type of includes to search for, can be 'editors'.
578 * The hook name to invoke.
580 * An optional include file name without .inc extension to limit the search to.
582 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
584 function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL
) {
585 // Determine implementations.
586 $directories = wysiwyg_get_directories($type);
587 $directories['wysiwyg_'] = wysiwyg_get_path($type);
588 $file_list = array();
589 foreach ($directories as
$module => $path) {
590 $file_list[$module] = drupal_system_listing("$file" .
'.inc$', $path, 'name', 0);
593 // Load implementations.
595 foreach (array_filter($file_list) as
$module => $files) {
596 foreach ($files as
$file) {
597 include_once
'./' .
$file->filename
;
598 $result = _wysiwyg_process_include('wysiwyg', $module .
$file->name
, dirname($file->filename
), $hook);
599 if (is_array($result)) {
600 $info = array_merge($info, $result);
608 * Helper function to build module/file paths.
611 * A file or directory in a module to return.
613 * Whether to prefix the resulting path with base_path().
615 * The module name to use as prefix.
618 * The path to the specified file in a module.
620 function wysiwyg_get_path($file = '', $base_path = FALSE
, $module = 'wysiwyg') {
621 $base_path = ($base_path ?
base_path() : '');
622 return $base_path .
drupal_get_path('module', $module) .
'/' .
$file;
626 * Return a list of directories by modules implementing wysiwyg_include_directory().
629 * The type of a plugin; can be 'editors'.
632 * An array containing module names suffixed with '_' and their defined
635 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
637 function wysiwyg_get_directories($plugintype) {
638 $directories = array();
639 foreach (module_implements('wysiwyg_include_directory') as
$module) {
640 $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
641 if (isset($result) && is_string($result)) {
642 $directories[$module .
'_'] = drupal_get_path('module', $module) .
'/'.
$result;
649 * Process a single hook implementation of a wysiwyg editor.
652 * The module that owns the hook.
654 * Either the module or 'wysiwyg_' . $file->name
656 * The name of the hook being invoked.
658 function _wysiwyg_process_include($module, $identifier, $path, $hook) {
659 $function = $identifier .
'_' .
$hook;
660 if (!function_exists($function)) {
663 $result = $function();
664 if (!isset($result) || !is_array($result)) {
669 foreach ($result as
$editor => $properties) {
670 $result[$editor]['module'] = $module;
671 $result[$editor]['name'] = $editor;
672 $result[$editor]['path'] = $path;
678 * @} End of "defgroup wysiwyg_api".