6 * Integrate Wysiwyg editors into Drupal.
10 * Form builder for Wysiwyg profile form.
12 function wysiwyg_profile_form($form_state, $profile) {
14 $profile = (array) $profile;
19 if (empty($profile['settings'])) {
20 $profile['settings'] = array();
22 $profile['settings'] += array(
24 'user_choose' => FALSE
,
25 'show_toggle' => TRUE
,
26 'theme' => 'advanced',
29 'access_pages' => "node/*\nuser/*\ncomment/*",
31 'toolbar_loc' => 'top',
32 'toolbar_align' => 'left',
33 'path_loc' => 'bottom',
35 // Also available, but buggy in TinyMCE 2.x: blockquote,code,dt,dd,samp.
36 'block_formats' => 'p,address,pre,h2,h3,h4,h5,h6,div',
37 'verify_html' => TRUE
,
38 'preformatted' => FALSE
,
39 'convert_fonts_to_spans' => TRUE
,
40 'remove_linebreaks' => TRUE
,
41 'apply_source_formatting' => FALSE
,
42 'paste_auto_cleanup_on_paste' => FALSE
,
43 'css_setting' => 'theme',
45 'css_classes' => NULL
,
47 $profile = (object) $profile;
49 $formats = filter_formats();
50 $editor = wysiwyg_get_editor($profile->editor
);
51 drupal_set_title(t('%editor profile for %format', array('%editor' => $editor['title'], '%format' => $formats[$profile->format
]->name
)));
54 $form['format'] = array('#type' => 'value', '#value' => $profile->format
);
55 $form['input_format'] = array('#type' => 'value', '#value' => $formats[$profile->format
]->name
);
56 $form['editor'] = array('#type' => 'value', '#value' => $profile->editor
);
58 $form['basic'] = array(
59 '#type' => 'fieldset',
60 '#title' => t('Basic setup'),
61 '#collapsible' => TRUE
,
65 $form['basic']['default'] = array(
66 '#type' => 'checkbox',
67 '#title' => t('Enabled by default'),
68 '#default_value' => $profile->settings
['default'],
70 '#description' => t('The default editor state for users having access to this profile. Users are able to override this state if the next option is enabled.'),
73 $form['basic']['user_choose'] = array(
74 '#type' => 'checkbox',
75 '#title' => t('Allow users to choose default'),
76 '#default_value' => $profile->settings
['user_choose'],
78 '#description' => t('If allowed, users will be able to choose their own editor default state in their user account settings.'),
81 $form['basic']['show_toggle'] = array(
82 '#type' => 'checkbox',
83 '#title' => t('Show <em>enable/disable rich text</em> toggle link'),
84 '#default_value' => $profile->settings
['show_toggle'],
86 '#description' => t('Whether or not to show the <em>enable/disable rich text</em> toggle link below a textarea. If disabled, the user setting or global default is used (see above).'),
89 $form['basic']['theme'] = array(
91 '#value' => $profile->settings
['theme'],
94 $form['basic']['language'] = array(
96 '#title' => t('Language'),
97 '#default_value' => $profile->settings
['language'],
98 '#options' => drupal_map_assoc(array('ar', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'fr_ca', 'he', 'hu', 'is', 'it', 'ja', 'ko', 'nb', 'nl', 'nn', 'pl', 'pt', 'pt_br', 'ru', 'ru_KOI8-R', 'ru_UTF-8', 'si', 'sk', 'sv', 'th', 'tr', 'uk', 'zh_cn', 'zh_tw', 'zh_tw_utf8')),
99 '#description' => t('The language to use for the editor interface. Language codes are based on the <a href="http://www.loc.gov/standards/iso639-2/englangn.html">ISO-639-2</a> format.'),
102 $form['buttons'] = array(
103 '#type' => 'fieldset',
104 '#title' => t('Buttons and plugins'),
105 '#collapsible' => TRUE
,
106 '#collapsed' => TRUE
,
108 '#theme' => 'wysiwyg_admin_button_table',
111 $plugins = wysiwyg_get_plugins($profile->editor
);
112 // Generate the button list.
113 foreach ($plugins as
$name => $meta) {
114 if (isset($meta['buttons']) && is_array($meta['buttons'])) {
115 foreach ($meta['buttons'] as
$button => $title) {
117 if (!empty($meta['path'])) {
118 // @todo Button icon locations are different in editors, editor versions,
119 // and contrib/custom plugins (like Image Assist, f.e.).
120 $img_src = $meta['path'] .
"/images/$name.gif";
121 // Handle plugins that have more than one button.
122 if (!file_exists($img_src)) {
123 $img_src = $meta['path'] .
"/images/$button.gif";
125 $icon = file_exists($img_src) ?
'<img src="' .
base_path() .
$img_src .
'" title="' .
$button .
'" style="border: 1px solid grey; vertical-align: middle;" />' : '';
127 $title = (isset($meta['url']) ?
l($title, $meta['url'], array('target' => '_blank')) : $title);
128 $title = (!empty($icon) ?
$icon .
' ' .
$title : $title);
129 $form['buttons'][$name][$button] = array(
130 '#type' => 'checkbox',
132 '#default_value' => !empty($profile->settings
['buttons'][$name][$button]) ?
$profile->settings
['buttons'][$name][$button] : FALSE
,
136 else if (isset($meta['extensions']) && is_array($meta['extensions'])) {
137 foreach ($meta['extensions'] as
$extension => $title) {
138 $form['buttons'][$name][$extension] = array(
139 '#type' => 'checkbox',
140 '#title' => isset($meta['url']) ?
l($title, $meta['url'], array('target' => '_blank')) : $title,
141 '#default_value' => !empty($profile->settings
['buttons'][$name][$extension]) ?
$profile->settings
['buttons'][$name][$extension] : FALSE
,
147 $form['appearance'] = array(
148 '#type' => 'fieldset',
149 '#title' => t('Editor appearance'),
150 '#collapsible' => TRUE
,
151 '#collapsed' => TRUE
,
154 $form['appearance']['toolbar_loc'] = array(
156 '#title' => t('Toolbar location'),
157 '#default_value' => $profile->settings
['toolbar_loc'],
158 '#options' => array('bottom' => t('Bottom'), 'top' => t('Top')),
159 '#description' => t('This option controls whether the editor toolbar is displayed above or below the editing area.'),
162 $form['appearance']['toolbar_align'] = array(
164 '#title' => t('Button alignment'),
165 '#default_value' => $profile->settings
['toolbar_align'],
166 '#options' => array('center' => t('Center'), 'left' => t('Left'), 'right' => t('Right')),
167 '#description' => t('This option controls the alignment of icons in the editor toolbar.'),
170 $form['appearance']['path_loc'] = array(
172 '#title' => t('Path location'),
173 '#default_value' => $profile->settings
['path_loc'],
174 '#options' => array('none' => t('Hide'), 'top' => t('Top'), 'bottom' => t('Bottom')),
175 '#description' => t('Where to display the path to HTML elements (i.e. <code>body > table > tr > td</code>).'),
178 $form['appearance']['resizing'] = array(
179 '#type' => 'checkbox',
180 '#title' => t('Enable resizing button'),
181 '#default_value' => $profile->settings
['resizing'],
182 '#return_value' => 1,
183 '#description' => t('This option gives you the ability to enable/disable the resizing button. If enabled, the Path location toolbar must be set to "Top" or "Bottom" in order to display the resize icon.'),
186 $form['output'] = array(
187 '#type' => 'fieldset',
188 '#title' => t('Cleanup and output'),
189 '#collapsible' => TRUE
,
190 '#collapsed' => TRUE
,
193 $form['output']['verify_html'] = array(
194 '#type' => 'checkbox',
195 '#title' => t('Verify HTML'),
196 '#default_value' => $profile->settings
['verify_html'],
197 '#return_value' => 1,
198 '#description' => t('If enabled, potentially malicious code like <code><HEAD></code> tags will be removed from HTML contents.'),
201 $form['output']['preformatted'] = array(
202 '#type' => 'checkbox',
203 '#title' => t('Preformatted'),
204 '#default_value' => $profile->settings
['preformatted'],
205 '#return_value' => 1,
206 '#description' => t('If enabled, the editor will insert TAB characters on tab and preserve other whitespace characters just like a PRE element in HTML does.'),
209 $form['output']['convert_fonts_to_spans'] = array(
210 '#type' => 'checkbox',
211 '#title' => t('Convert <font> tags to styles'),
212 '#default_value' => $profile->settings
['convert_fonts_to_spans'],
213 '#return_value' => 1,
214 '#description' => t('If enabled, HTML tags declaring the font size, font family, font color and font background color will be replaced by inline CSS styles.'),
217 $form['output']['remove_linebreaks'] = array(
218 '#type' => 'checkbox',
219 '#title' => t('Remove linebreaks'),
220 '#default_value' => $profile->settings
['remove_linebreaks'],
221 '#return_value' => 1,
222 '#description' => t('If enabled, the editor will remove most linebreaks from contents. Disabling this option could avoid conflicts with other input filters.'),
225 $form['output']['apply_source_formatting'] = array(
226 '#type' => 'checkbox',
227 '#title' => t('Apply source formatting'),
228 '#default_value' => $profile->settings
['apply_source_formatting'],
229 '#return_value' => 1,
230 '#description' => t('If enabled, the editor will re-format the HTML source code. Disabling this option could avoid conflicts with other input filters.'),
233 $form['output']['paste_auto_cleanup_on_paste'] = array(
234 '#type' => 'checkbox',
235 '#title' => t('Force cleanup on standard paste'),
236 '#default_value' => $profile->settings
['paste_auto_cleanup_on_paste'],
237 '#return_value' => 1,
238 '#description' => t('If enabled, the default paste function (CTRL-V or SHIFT-INS) behaves like the "paste from word" plugin function.'),
241 $form['css'] = array(
242 '#type' => 'fieldset',
243 '#title' => t('CSS'),
244 '#collapsible' => TRUE
,
245 '#collapsed' => TRUE
,
248 $form['css']['block_formats'] = array(
249 '#type' => 'textfield',
250 '#title' => t('Block formats'),
251 '#default_value' => $profile->settings
['block_formats'],
254 '#description' => t('Comma separated list of HTML block formats. Possible values: <code>@format-list</code>.', array('@format-list' => 'p,h1,h2,h3,h4,h5,h6,div,blockquote,address,pre,code,dt,dd')),
257 $form['css']['css_setting'] = array(
259 '#title' => t('Editor CSS'),
260 '#default_value' => $profile->settings
['css_setting'],
261 '#options' => array('theme' => t('Use theme CSS'), 'self' => t('Define CSS'), 'none' => t('Editor default CSS')),
262 '#description' => t('Defines the CSS to be used in the editor area.<br />Use theme CSS - loads stylesheets from current site theme.<br/>Define CSS - enter path for stylesheet files below.<br />Editor default CSS - uses default stylesheets from editor.'),
265 $form['css']['css_path'] = array(
266 '#type' => 'textfield',
267 '#title' => t('CSS path'),
268 '#default_value' => $profile->settings
['css_path'],
271 '#description' => t('If "Define CSS" was selected above, enter path to a CSS file or a list of CSS files separated by a comma.') .
'<br />' .
t('Available tokens: <code>%b</code> (base path, eg: <code>/</code>), <code>%t</code> (path to theme, eg: <code>themes/garland</code>)') .
'<br />' .
t('Example:') .
' css/editor.css,/themes/garland/style.css,%b%t/style.css,http://example.com/external.css',
274 $form['css']['css_classes'] = array(
275 '#type' => 'textarea',
276 '#title' => t('CSS classes'),
277 '#default_value' => $profile->settings
['css_classes'],
278 '#description' => t('Optionally define CSS classes for the "Font style" dropdown list.<br />Enter one class on each line in the format: !format. Example: !example<br />If left blank, CSS classes are automatically imported from all loaded stylesheet(s).', array('!format' => '<code>[title]=[class]</code>', '!example' => 'My heading=header1')),
281 $form['submit'] = array(
283 '#value' => t('Save'),
290 * Submit callback for Wysiwyg profile form.
292 * @see wysiwyg_profile_form()
294 function wysiwyg_profile_form_submit($form, &$form_state) {
295 $values = $form_state['values'];
296 if (isset($values['buttons'])) {
297 // Store only enabled buttons for each plugin.
298 foreach ($values['buttons'] as
$plugin => $buttons) {
299 $values['buttons'][$plugin] = array_filter($values['buttons'][$plugin]);
301 // Store only enabled plugins.
302 $values['buttons'] = array_filter($values['buttons']);
304 // Remove input format name.
305 $format = $values['format'];
306 $input_format = $values['input_format'];
307 $editor = $values['editor'];
308 unset($values['format'], $values['input_format'], $values['editor']);
310 // Remove FAPI values.
311 // @see system_settings_form_submit()
312 unset($values['submit'], $values['form_id'], $values['op'], $values['form_token'], $values['form_build_id']);
314 // Insert new profile data.
315 db_query("UPDATE {wysiwyg} SET settings = '%s' WHERE format = %d", serialize($values), $format);
317 drupal_set_message(t('Wysiwyg profile for %format has been saved.', array('%format' => $input_format)));
319 $form_state['redirect'] = 'admin/settings/wysiwyg';
323 * Layout for the buttons in the Wysiwyg Editor profile form.
325 function theme_wysiwyg_admin_button_table($form) {
328 // Flatten forms array.
329 foreach (element_children($form) as
$name) {
330 foreach (element_children($form[$name]) as
$button) {
331 $buttons[] = drupal_render($form[$name][$button]);
335 // Split checkboxes into rows with 3 columns.
336 $total = count($buttons);
338 for ($i = 0; $i < $total; $i++) {
340 $row[] = array('data' => $buttons[$i]);
341 if (isset($buttons[++$i])) {
342 $row[] = array('data' => $buttons[$i]);
344 if (isset($buttons[++$i])) {
345 $row[] = array('data' => $buttons[$i]);
350 $output = theme('table', array(), $rows, array('width' => '100%'));
356 * Display overview of setup Wysiwyg Editor profiles; menu callback.
358 function wysiwyg_profile_overview() {
359 include_once
'./includes/install.inc';
362 // Check which wysiwyg editors are installed.
363 $editors = wysiwyg_get_all_editors();
364 $count = count($editors);
366 $options = array('' => t('No editor'));
368 foreach ($editors as
$name => $editor) {
369 $status[$name] = array(
370 'severity' => (isset($editor['error']) ? REQUIREMENT_ERROR
: ($editor['installed'] ? REQUIREMENT_OK
: REQUIREMENT_INFO
)),
371 'title' => t('<a href="!vendor-url">@editor</a> (<a href="!download-url">Download</a>)', array('!vendor-url' => $editor['vendor url'], '@editor' => $editor['title'], '!download-url' => $editor['download url'])),
372 'value' => (isset($editor['installed version']) ?
$editor['installed version'] : t('Not installed.')),
373 'description' => (isset($editor['error']) ?
$editor['error'] : ''),
375 if ($editor['installed']) {
376 $options[$name] = $editor['title'] .
(isset($editor['installed version']) ?
' ' .
$editor['installed version'] : '');
379 // Build on-site installation instructions.
380 // @todo Setup $library in wysiwyg_load_editor() already.
381 $library = (isset($editor['library']) ?
$editor['library'] : key($editor['libraries']));
383 '@editor-path' => $editor['editor path'],
384 '@library-filepath' => $editor['library path'] .
'/' .
(isset($editor['libraries'][$library]['files'][0]) ?
$editor['libraries'][$library]['files'][0] : key($editor['libraries'][$library]['files'])),
386 $instructions = '<p>' .
t('Extract the archive and copy its contents into a new folder in the following location:<br /><code>@editor-path</code>', $targs) .
'</p>';
387 $instructions .
= '<p>' .
t('So the actual library can be found at:<br /><code>@library-filepath</code>', $targs) .
'</p>';
389 $status[$name]['description'] .
= $instructions;
392 // In case there is an error, always show installation instructions.
393 if (isset($editor['error'])) {
394 $show_instructions = TRUE
;
398 $show_instructions = TRUE
;
400 $form['status'] = array(
401 '#type' => 'fieldset',
402 '#title' => t('Installation instructions'),
403 '#collapsible' => TRUE
,
404 '#collapsed' => !isset($show_instructions),
405 '#description' => (!$count ?
t('There are no editor libraries installed currently. The following list contains a list of currently supported editors:') : ''),
408 $form['status']['report'] = array('#type' => 'markup', '#value' => theme('status_report', $status));
414 $formats = filter_formats();
415 $profiles = wysiwyg_profile_load_all();
416 $form['formats'] = array(
417 '#type' => 'fieldset',
418 '#title' => t('Wysiwyg profiles'),
419 '#description' => t('Once an editor has been associated with an input format, the editor association cannot be changed without first deleting the profile and then creating a new one. Delete a profile by clicking on the "delete" link and afterwards, set up a new profile as usual.'),
423 $enable_save = FALSE
;
424 foreach ($formats as
$id => $format) {
425 $form['formats'][$id]['name'] = array(
426 '#value' => check_plain($format->name
),
428 // Only display editor selection for associated input formats to avoid
429 // confusion about disabled selection.
430 if (isset($profiles[$id]) && !empty($profiles[$id]->editor
)) {
431 $form['formats'][$id]['editor'] = array(
433 '#value' => $options[$profiles[$id]->editor
],
434 '#id' => "edit-editor-$id",
438 $form['formats'][$id]['editor'] = array(
440 '#default_value' => '',
441 '#options' => $options,
442 '#id' => "edit-editor-$id",
446 if (isset($profiles[$id]) && !empty($profiles[$id]->editor
)) {
447 $form['formats'][$id]['edit'] = array(
448 '#value' => l(t('Edit'), "admin/settings/wysiwyg/profile/$id/edit"),
450 $form['formats'][$id]['delete'] = array(
451 '#value' => l(t('Delete'), "admin/settings/wysiwyg/profile/$id/delete"),
456 // Submitting the form when no editors can be selected causes errors.
458 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
464 * Return HTML for the Wysiwyg profile overview form.
466 function theme_wysiwyg_profile_overview($form) {
467 if (!isset($form['formats'])) {
471 $header = array(t('Input format'), t('Editor'), array('data' => t('Operations'), 'colspan' => 2));
473 foreach (element_children($form['formats']) as
$item) {
474 $format = &$form['formats'][$item];
476 drupal_render($format['name']),
477 drupal_render($format['editor']),
478 isset($format['edit']) ?
drupal_render($format['edit']) : '',
479 isset($format['delete']) ?
drupal_render($format['delete']) : '',
482 $form['formats']['#children'] = theme('table', $header, $rows);
483 $output .
= drupal_render($form);
488 * Submit callback for Wysiwyg profile overview form.
490 function wysiwyg_profile_overview_submit($form, &$form_state) {
491 foreach ($form_state['values']['formats'] as
$format => $values) {
492 db_query("UPDATE {wysiwyg} SET editor = '%s' WHERE format = %d", $values['editor'], $format);
493 if (!db_affected_rows()) {
494 db_query("INSERT INTO {wysiwyg} (format, editor) VALUES (%d, '%s')", $format, $values['editor']);
500 * Delete editor profile confirmation form.
502 function wysiwyg_profile_delete_confirm(&$form_state, $profile) {
503 $formats = filter_formats();
504 $format = $formats[$profile->format
];
505 $form['format'] = array('#type' => 'value', '#value' => $format);
508 t('Are you sure you want to remove the profile for %name?', array('%name' => $format->name
)),
509 'admin/settings/wysiwyg',
510 t('This action cannot be undone.'), t('Remove'), t('Cancel')
515 * Submit callback for Wysiwyg profile delete form.
517 * @see wysiwyg_profile_delete_confirm()
519 function wysiwyg_profile_delete_confirm_submit($form, &$form_state) {
520 $format = $form_state['values']['format'];
521 wysiwyg_profile_delete($format->format
);
522 drupal_set_message(t('Wysiwyg profile for %name has been deleted.', array('%name' => $format->name
)));
523 $form_state['redirect'] = 'admin/settings/wysiwyg';
527 * Remove a profile from the database.
529 function wysiwyg_profile_delete($format) {
530 db_query("DELETE FROM {wysiwyg} WHERE format = %d", $format);