6 * Integrate Wysiwyg editors into Drupal.
10 * Form builder for Wysiwyg profile form.
12 function wysiwyg_profile_form($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
)), PASS_THROUGH
);
53 $form['format'] = array('#type' => 'value', '#value' => $profile->format
);
54 $form['input_format'] = array('#type' => 'value', '#value' => $formats[$profile->format
]->name
);
55 $form['editor'] = array('#type' => 'value', '#value' => $profile->editor
);
57 $form['basic'] = array(
58 '#type' => 'fieldset',
59 '#title' => t('Basic setup'),
60 '#collapsible' => TRUE
,
64 $form['basic']['default'] = array(
65 '#type' => 'checkbox',
66 '#title' => t('Enabled by default'),
67 '#default_value' => $profile->settings
['default'],
69 '#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.'),
72 $form['basic']['user_choose'] = array(
73 '#type' => 'checkbox',
74 '#title' => t('Allow users to choose default'),
75 '#default_value' => $profile->settings
['user_choose'],
77 '#description' => t('If allowed, users will be able to choose their own editor default state in their user account settings.'),
80 $form['basic']['show_toggle'] = array(
81 '#type' => 'checkbox',
82 '#title' => t('Show <em>enable/disable rich text</em> toggle link'),
83 '#default_value' => $profile->settings
['show_toggle'],
85 '#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).'),
88 $form['basic']['theme'] = array(
90 '#value' => $profile->settings
['theme'],
93 $form['basic']['language'] = array(
95 '#title' => t('Language'),
96 '#default_value' => $profile->settings
['language'],
97 '#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', 'zh_cn', 'zh_tw', 'zh_tw_utf8')),
98 '#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.'),
101 $form['buttons'] = array(
102 '#type' => 'fieldset',
103 '#title' => t('Buttons and plugins'),
104 '#collapsible' => TRUE
,
105 '#collapsed' => TRUE
,
107 '#theme' => 'wysiwyg_admin_button_table',
110 $plugins = wysiwyg_get_plugins($profile->editor
);
111 // Generate the button list.
112 foreach ($plugins as
$name => $meta) {
113 if (isset($meta['buttons']) && is_array($meta['buttons'])) {
114 foreach ($meta['buttons'] as
$button => $title) {
116 if (!empty($meta['path'])) {
117 // @todo Button icon locations are different in editors, editor versions,
118 // and contrib/custom plugins (like Image Assist, f.e.).
119 $img_src = $meta['path'] .
"/images/$name.gif";
120 // Handle plugins that have more than one button.
121 if (!file_exists($img_src)) {
122 $img_src = $meta['path'] .
"/images/$button.gif";
124 $icon = file_exists($img_src) ?
'<img src="' .
base_path() .
$img_src .
'" title="' .
$button .
'" style="border: 1px solid grey; vertical-align: middle;" />' : '';
126 $title = (isset($meta['url']) ?
l($title, $meta['url'], array('target' => '_blank')) : $title);
127 $title = (!empty($icon) ?
$icon .
' ' .
$title : $title);
128 $form['buttons'][$name][$button] = array(
129 '#type' => 'checkbox',
131 '#default_value' => !empty($profile->settings
['buttons'][$name][$button]) ?
$profile->settings
['buttons'][$name][$button] : FALSE
,
135 else if (isset($meta['extensions']) && is_array($meta['extensions'])) {
136 foreach ($meta['extensions'] as
$extension => $title) {
137 $form['buttons'][$name][$extension] = array(
138 '#type' => 'checkbox',
139 '#title' => isset($meta['url']) ?
l($title, $meta['url'], array('target' => '_blank')) : $title,
140 '#default_value' => !empty($profile->settings
['buttons'][$name][$extension]) ?
$profile->settings
['buttons'][$name][$extension] : FALSE
,
146 $form['appearance'] = array(
147 '#type' => 'fieldset',
148 '#title' => t('Editor appearance'),
149 '#collapsible' => TRUE
,
150 '#collapsed' => TRUE
,
153 $form['appearance']['toolbar_loc'] = array(
155 '#title' => t('Toolbar location'),
156 '#default_value' => $profile->settings
['toolbar_loc'],
157 '#options' => array('bottom' => t('Bottom'), 'top' => t('Top')),
158 '#description' => t('This option controls whether the editor toolbar is displayed above or below the editing area.'),
161 $form['appearance']['toolbar_align'] = array(
163 '#title' => t('Button alignment'),
164 '#default_value' => $profile->settings
['toolbar_align'],
165 '#options' => array('center' => t('Center'), 'left' => t('Left'), 'right' => t('Right')),
166 '#description' => t('This option controls the alignment of icons in the editor toolbar.'),
169 $form['appearance']['path_loc'] = array(
171 '#title' => t('Path location'),
172 '#default_value' => $profile->settings
['path_loc'],
173 '#options' => array('none' => t('Hide'), 'top' => t('Top'), 'bottom' => t('Bottom')),
174 '#description' => t('Where to display the path to HTML elements (i.e. <code>body > table > tr > td</code>).'),
177 $form['appearance']['resizing'] = array(
178 '#type' => 'checkbox',
179 '#title' => t('Enable resizing button'),
180 '#default_value' => $profile->settings
['resizing'],
181 '#return_value' => 1,
182 '#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.'),
185 $form['output'] = array(
186 '#type' => 'fieldset',
187 '#title' => t('Cleanup and output'),
188 '#collapsible' => TRUE
,
189 '#collapsed' => TRUE
,
192 $form['output']['verify_html'] = array(
193 '#type' => 'checkbox',
194 '#title' => t('Verify HTML'),
195 '#default_value' => $profile->settings
['verify_html'],
196 '#return_value' => 1,
197 '#description' => t('If enabled, potentially malicious code like <code><HEAD></code> tags will be removed from HTML contents.'),
200 $form['output']['preformatted'] = array(
201 '#type' => 'checkbox',
202 '#title' => t('Preformatted'),
203 '#default_value' => $profile->settings
['preformatted'],
204 '#return_value' => 1,
205 '#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.'),
208 $form['output']['convert_fonts_to_spans'] = array(
209 '#type' => 'checkbox',
210 '#title' => t('Convert <font> tags to styles'),
211 '#default_value' => $profile->settings
['convert_fonts_to_spans'],
212 '#return_value' => 1,
213 '#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.'),
216 $form['output']['remove_linebreaks'] = array(
217 '#type' => 'checkbox',
218 '#title' => t('Remove linebreaks'),
219 '#default_value' => $profile->settings
['remove_linebreaks'],
220 '#return_value' => 1,
221 '#description' => t('If enabled, the editor will remove most linebreaks from contents. Disabling this option could avoid conflicts with other input filters.'),
224 $form['output']['apply_source_formatting'] = array(
225 '#type' => 'checkbox',
226 '#title' => t('Apply source formatting'),
227 '#default_value' => $profile->settings
['apply_source_formatting'],
228 '#return_value' => 1,
229 '#description' => t('If enabled, the editor will re-format the HTML source code. Disabling this option could avoid conflicts with other input filters.'),
232 $form['output']['paste_auto_cleanup_on_paste'] = array(
233 '#type' => 'checkbox',
234 '#title' => t('Force cleanup on standard paste'),
235 '#default_value' => $profile->settings
['paste_auto_cleanup_on_paste'],
236 '#return_value' => 1,
237 '#description' => t('If enabled, the default paste function (CTRL-V or SHIFT-INS) behaves like the "paste from word" plugin function.'),
240 $form['css'] = array(
241 '#type' => 'fieldset',
242 '#title' => t('CSS'),
243 '#collapsible' => TRUE
,
244 '#collapsed' => TRUE
,
247 $form['css']['block_formats'] = array(
248 '#type' => 'textfield',
249 '#title' => t('Block formats'),
250 '#default_value' => $profile->settings
['block_formats'],
253 '#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')),
256 $form['css']['css_setting'] = array(
258 '#title' => t('Editor CSS'),
259 '#default_value' => $profile->settings
['css_setting'],
260 '#options' => array('theme' => t('Use theme CSS'), 'self' => t('Define CSS'), 'none' => t('Editor default CSS')),
261 '#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.'),
264 $form['css']['css_path'] = array(
265 '#type' => 'textfield',
266 '#title' => t('CSS path'),
267 '#default_value' => $profile->settings
['css_path'],
270 '#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',
273 $form['css']['css_classes'] = array(
274 '#type' => 'textarea',
275 '#title' => t('CSS classes'),
276 '#default_value' => $profile->settings
['css_classes'],
277 '#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')),
280 $form['submit'] = array(
282 '#value' => t('Save'),
289 * Submit callback for Wysiwyg profile form.
291 * @see wysiwyg_profile_form()
293 function wysiwyg_profile_form_submit($form, &$form_state) {
294 $values = $form_state['values'];
295 if (isset($values['buttons'])) {
296 // Store only enabled buttons for each plugin.
297 foreach ($values['buttons'] as
$plugin => $buttons) {
298 $values['buttons'][$plugin] = array_filter($values['buttons'][$plugin]);
300 // Store only enabled plugins.
301 $values['buttons'] = array_filter($values['buttons']);
303 // Remove input format name.
304 $format = $values['format'];
305 $input_format = $values['input_format'];
306 $editor = $values['editor'];
307 unset($values['format'], $values['input_format'], $values['editor']);
309 // Remove FAPI values.
310 // @see system_settings_form_submit()
311 unset($values['submit'], $values['form_id'], $values['op'], $values['form_token'], $values['form_build_id']);
313 // Insert new profile data.
314 db_query("UPDATE {wysiwyg} SET settings = :settings WHERE format = :format", array(':settings' => serialize($values), ':format' => $format));
316 drupal_set_message(t('Wysiwyg profile for %format has been saved.', array('%format' => $input_format)));
318 $form_state['redirect'] = 'admin/config/content/wysiwyg';
322 * Layout for the buttons in the Wysiwyg Editor profile form.
324 function theme_wysiwyg_admin_button_table($form) {
327 // Flatten forms array.
328 foreach (element_children($form) as
$name) {
329 foreach (element_children($form[$name]) as
$button) {
330 $buttons[] = drupal_render($form[$name][$button]);
334 // Split checkboxes into rows with 3 columns.
335 $total = count($buttons);
337 for ($i = 0; $i < $total; $i++) {
339 $row[] = array('data' => $buttons[$i]);
340 if (isset($buttons[++$i])) {
341 $row[] = array('data' => $buttons[$i]);
343 if (isset($buttons[++$i])) {
344 $row[] = array('data' => $buttons[$i]);
349 $output = theme('table', array(), $rows, array('width' => '100%'));
355 * Display overview of setup Wysiwyg Editor profiles; menu callback.
357 function wysiwyg_profile_overview($form, &$form_state) {
358 include_once
'./includes/install.inc';
360 // Check which wysiwyg editors are installed.
361 $editors = wysiwyg_get_all_editors();
362 $count = count($editors);
364 $options = array('' => t('No editor'));
366 foreach ($editors as
$name => $editor) {
367 $status[$name] = array(
368 'severity' => (isset($editor['error']) ? REQUIREMENT_ERROR
: ($editor['installed'] ? REQUIREMENT_OK
: REQUIREMENT_INFO
)),
369 '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'])),
370 'value' => (isset($editor['installed version']) ?
$editor['installed version'] : t('Not installed.')),
371 'description' => (isset($editor['error']) ?
$editor['error'] : ''),
373 if ($editor['installed']) {
374 $options[$name] = $editor['title'] .
(isset($editor['installed version']) ?
' ' .
$editor['installed version'] : '');
377 // Build on-site installation instructions.
378 // @todo Setup $library in wysiwyg_load_editor() already.
379 $library = (isset($editor['library']) ?
$editor['library'] : key($editor['libraries']));
381 '@editor-path' => $editor['editor path'],
382 '@library-filepath' => $editor['library path'] .
'/' .
$editor['libraries'][$library]['files'][0],
384 $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>';
385 $instructions .
= '<p>' .
t('So the actual library can be found at:<br /><code>@library-filepath</code>', $targs) .
'</p>';
387 $status[$name]['description'] .
= $instructions;
390 // In case there is an error, always show installation instructions.
391 if (isset($editor['error'])) {
392 $show_instructions = TRUE
;
396 $show_instructions = TRUE
;
398 $form['status'] = array(
399 '#type' => 'fieldset',
400 '#title' => t('Installation instructions'),
401 '#collapsible' => TRUE
,
402 '#collapsed' => !isset($show_instructions),
403 '#description' => (!$count ?
t('There are no editor libraries installed currently. The following list contains a list of currently supported editors:') : ''),
406 $form['status']['report'] = array('#markup' => theme('status_report', $status));
412 $formats = filter_formats();
413 $profiles = wysiwyg_profile_load_all();
414 $form['formats'] = array(
416 '#description' => t('To assign a different editor to a text format, click "delete" to remove the existing first.'),
419 foreach ($formats as
$id => $format) {
420 $form['formats'][$id]['name'] = array(
421 '#markup' => check_plain($format->name
),
423 // Only display editor selection for associated input formats to avoid
424 // confusion about disabled selection.
425 if (isset($profiles[$id]) && !empty($profiles[$id]->editor
)) {
426 $form['formats'][$id]['editor'] = array(
427 '#markup' => $options[$profiles[$id]->editor
],
431 $form['formats'][$id]['editor'] = array(
433 '#default_value' => '',
434 '#options' => $options,
437 if (isset($profiles[$id]) && !empty($profiles[$id]->editor
)) {
438 $form['formats'][$id]['edit'] = array(
439 '#markup' => l(t('Edit'), "admin/config/content/wysiwyg/profile/$id/edit"),
441 $form['formats'][$id]['delete'] = array(
442 '#markup' => l(t('Delete'), "admin/config/content/wysiwyg/profile/$id/delete"),
447 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
452 * Return HTML for the Wysiwyg profile overview form.
454 function theme_wysiwyg_profile_overview($form) {
455 if (!isset($form['formats'])) {
459 $header = array(t('Input format'), t('Editor'), array('data' => t('Operations'), 'colspan' => 2));
461 foreach (element_children($form['formats']) as
$item) {
462 $format = &$form['formats'][$item];
464 drupal_render($format['name']),
465 drupal_render($format['editor']),
466 isset($format['edit']) ?
drupal_render($format['edit']) : '',
467 isset($format['delete']) ?
drupal_render($format['delete']) : '',
470 $form['formats']['table']['#markup'] = theme('table', $header, $rows);
471 $output .
= drupal_render_children($form);
476 * Submit callback for Wysiwyg profile overview form.
478 function wysiwyg_profile_overview_submit($form, &$form_state) {
479 foreach ($form_state['values']['formats'] as
$format => $values) {
481 ->key(array('format' => $format))
483 'editor' => $values['editor'],
490 * Delete editor profile confirmation form.
492 function wysiwyg_profile_delete_confirm($form, &$form_state, $profile) {
493 $formats = filter_formats();
494 $format = $formats[$profile->format
];
495 $form['format'] = array('#type' => 'value', '#value' => $format);
498 t('Are you sure you want to remove the profile for %name?', array('%name' => $format->name
)),
499 'admin/config/content/wysiwyg',
500 t('This action cannot be undone.'), t('Remove'), t('Cancel')
505 * Submit callback for Wysiwyg profile delete form.
507 * @see wysiwyg_profile_delete_confirm()
509 function wysiwyg_profile_delete_confirm_submit($form, &$form_state) {
510 $format = $form_state['values']['format'];
511 wysiwyg_profile_delete($format->format
);
512 drupal_set_message(t('Wysiwyg profile for %name has been deleted.', array('%name' => $format->name
)));
513 $form_state['redirect'] = 'admin/config/content/wysiwyg';
517 * Remove a profile from the database.
519 function wysiwyg_profile_delete($format) {
521 ->condition('format', $format)