6 * Plugin implementation of hook_editor().
8 * - Function signature and returned editor name nust match include filename.
9 * - Returns an array of supported editor versions along with support files.
11 * @todo wysiwyg_<editor>_alter() to add/inject optional libraries like gzip.
13 function wysiwyg_tinymce_editor() {
15 $editor['tinymce'] = array(
16 // Required properties
18 'vendor url' => 'http://tinymce.moxiecode.com',
19 'download url' => 'http://tinymce.moxiecode.com/download.php',
20 'library path' => wysiwyg_get_path('tinymce') .
'/jscripts/tiny_mce',
21 'libraries' => array( // We cannot assume that all editors need just one js library.
22 '' => array( // Key may be used in wysiwyg_tinymce_settings() for exec mode.
23 'title' => 'Minified',
24 'files' => array('tiny_mce.js'),
28 'files' => array('tiny_mce_src.js'),
31 'version callback' => 'wysiwyg_tinymce_version',
32 'themes callback' => 'wysiwyg_tinymce_themes',
33 'settings callback' => 'wysiwyg_tinymce_settings',
34 'plugin callback' => 'wysiwyg_tinymce_plugins',
35 'plugin settings callback' => 'wysiwyg_tinymce_plugin_settings',
36 'versions' => array( // Each version can override global editor properties.
38 // 'include files' => array('tinymce-2.inc'),
39 'js files' => array('tinymce-2.js'),
40 'css files' => array('tinymce-2.css'),
41 'download url' => 'http://sourceforge.net/project/showfiles.php?group_id=103281&package_id=111430&release_id=557383',
44 // 'include files' => array('tinymce-3.inc'),
45 'js files' => array('tinymce-3.js'),
46 'css files' => array('tinymce-3.css'),
47 // 'plugin callback' => 'wysiwyg_tinymce_3_plugins',
50 'title' => 'Minified',
51 'files' => array('tiny_mce.js'),
55 'files' => array('tiny_mce_jquery.js'),
59 'files' => array('tiny_mce_src.js'),
64 // Optional properties
65 // 'editor path' => wysiwyg_get_path('tinymce'), // Assumed by default.
66 // 'js path' => wysiwyg_get_path('editors/js'), // Assumed by default.
67 // 'css path' => wysiwyg_get_path('editors/css'), // Assumed by default.
73 * Detect editor version.
76 * An array containing editor properties as returned from hook_editor().
79 * The installed editor version.
81 function wysiwyg_tinymce_version($editor) {
82 $changelog = wysiwyg_get_path('tinymce') .
'/changelog.txt';
83 if (!file_exists($changelog)) {
84 $changelog = wysiwyg_get_path('tinymce') .
'/changelog';
86 $changelog = fopen($changelog, 'r');
87 $line = fgets($changelog, 50);
88 if (preg_match('@^Version ([\d\.]+)@', $line, $version)) {
95 * Return runtime editor settings for a given wysiwyg profile.
98 * A processed hook_editor() array of editor properties.
100 * An array containing wysiwyg editor profile settings.
102 * The name of a theme/GUI/skin to use.
105 * A settings array to be populated in
106 * Drupal.settings.wysiwyg.configs.{editor}
108 function wysiwyg_tinymce_settings($editor, $config, $theme) {
110 'apply_source_formatting' => $config['apply_source_formatting'],
111 'button_tile_map' => TRUE
, // @todo Add a setting for this.
112 'convert_fonts_to_spans' => $config['convert_fonts_to_spans'],
113 'document_base_url' => base_path(),
114 'entity_encoding' => 'raw',
115 'language' => $config['language'],
117 'paste_auto_cleanup_on_paste' => $config['paste_auto_cleanup_on_paste'],
118 'plugins' => array(),
119 'preformatted' => $config['preformatted'],
120 'relative_urls' => FALSE
,
121 'remove_linebreaks' => $config['remove_linebreaks'],
123 'verify_html' => $config['verify_html'],
127 if ($config['css_classes']) {
128 $init['theme_advanced_styles'] = implode(';', array_filter(explode("\n", str_replace("\r", '', $config['css_classes']))));
131 if ($config['css_setting'] == 'theme') {
132 $css = path_to_theme() .
'/style.css';
133 if (file_exists($css)) {
134 $init['content_css'] = base_path() .
$css;
137 else if ($config['css_setting'] == 'self') {
138 $init['content_css'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
141 // Find the enabled buttons and the button row they belong on.
142 // Also map the plugin metadata for each button.
143 // @todo What follows is a pain; needs a rewrite.
144 if (!empty($config['buttons']) && is_array($config['buttons'])) {
145 // $init['buttons'] are stacked into $init['theme_advanced_buttons1'] later.
146 // @todo Add a toolbar designer based on jQuery UI.
147 $init['buttons'] = array();
148 // Only array keys in $init['extensions'] matter; added to $init['plugins']
150 $init['extensions'] = array();
151 // $init['extended_valid_elements'] are just stacked, unique'd later, and
152 // transformed into a comma-separated string in wysiwyg_add_editor_settings().
153 // @todo Needs a complete plugin API redesign using arrays for
154 // tag => attributes definitions and array_merge_recursive().
155 $init['extended_valid_elements'] = array();
157 $plugins = wysiwyg_get_plugins($editor['name']);
158 foreach ($config['buttons'] as
$plugin => $buttons) {
159 foreach ($buttons as
$button => $enabled) {
160 // Iterate separately over buttons and extensions properties.
161 foreach (array('buttons', 'extensions') as
$type) {
162 // Skip unavailable plugins.
163 if (!isset($plugins[$plugin][$type][$button])) {
167 if ($type == 'buttons') {
168 $init['buttons'][] = $button;
170 // Add external plugins to the list of extensions.
171 if ($type == 'buttons' && !isset($plugins[$plugin]['internal'])) {
172 $init['extensions'][_wysiwyg_tinymce_plugin_name('add', $plugin)] = 1;
174 // Add internal buttons that also need to be loaded as extension.
175 else if ($type == 'buttons' && isset($plugins[$plugin]['load'])) {
176 $init['extensions'][$plugin] = 1;
178 // Add plain extensions.
179 else if ($type == 'extensions') {
180 $init['extensions'][$plugin] = 1;
182 // Allow plugins to add valid HTML elements.
183 if (!empty($plugins[$plugin]['extended_valid_elements'])) {
184 $init['extended_valid_elements'] = array_merge($init['extended_valid_elements'], $plugins[$plugin]['extended_valid_elements']);
186 // Allow plugins to add or override global configuration settings.
187 if (!empty($plugins[$plugin]['options'])) {
188 $init = array_merge($init, $plugins[$plugin]['options']);
194 $init['extended_valid_elements'] = array_unique($init['extended_valid_elements']);
195 if ($init['extensions']) {
196 $init['plugins'] = array_keys($init['extensions']);
197 unset($init['extensions']);
199 unset($init['extensions']);
203 // Add theme-specific settings.
207 'theme_advanced_blockformats' => $config['block_formats'] ?
$config['block_formats'] : 'p,h2,h3,h4,h5,h6',
208 'theme_advanced_path_location' => $config['path_loc'],
209 'theme_advanced_resizing' => $config['resizing'],
210 'theme_advanced_resize_horizontal' => FALSE
,
211 'theme_advanced_resizing_use_cookie' => FALSE
,
212 'theme_advanced_toolbar_location' => $config['toolbar_loc'],
213 'theme_advanced_toolbar_align' => $config['toolbar_align'],
214 // Note: These rows need to be set to NULL otherwise TinyMCE loads its
215 // own buttons as defined in advanced theme.
216 'theme_advanced_buttons1' => array(),
217 'theme_advanced_buttons2' => array(),
218 'theme_advanced_buttons3' => array(),
221 for ($i = 0; $i < count($init['buttons']); $i++) {
222 $init['theme_advanced_buttons1'][] = $init['buttons'][$i];
226 unset($init['buttons']);
228 // Convert the config values into the form expected by TinyMCE.
229 foreach ($init as
$key => $value) {
230 if (is_bool($value)) {
233 if (is_array($value)) {
234 $init[$key] = implode(',', $init[$key]);
242 * Determine available editor themes or check/reset a given one.
245 * A processed hook_editor() array of editor properties.
247 * A wysiwyg editor profile.
250 * An array of theme names. The first returned name should be the default
253 function wysiwyg_tinymce_themes($editor, $profile) {
256 $dir = $editor['library path'] . '/themes/';
257 if (is_dir($dir) && $dh = opendir($dir)) {
258 while (($file = readdir($dh)) !== FALSE) {
259 if (!in_array($file, array('.', '..', 'CVS', '.svn')) && is_dir($dir . $file)) {
260 $themes[$file] = $file;
268 return array('advanced', 'simple');
272 * Build a JS settings array of external plugins that need to be loaded separately.
274 * TinyMCE requires that external plugins (i.e. not residing in the editor's
275 * directory) are loaded (once) after the editor has been initialized.
277 function wysiwyg_tinymce_plugin_settings($editor, $profile, $plugins) {
279 foreach ($plugins as
$name => $plugin) {
280 if (!isset($plugin['internal'])) {
281 $settings[$name] = base_path() .
$plugin['path'];
288 * Add or remove leading hiven to/of external plugin names.
290 * TinyMCE requires that external plugins, which should not be loaded from
291 * its own plugin repository are prefixed with a hiven in the name.
294 * Operation to perform, 'add' or 'remove' (hiven).
295 * @param string $name
298 function _wysiwyg_tinymce_plugin_name($op, $name) {
300 if (strpos($name, '-') !== 0) {
305 else if ($op == 'remove') {
306 if (strpos($name, '-') === 0) {
307 return substr($name, 1);
314 * Return internal plugins for TinyMCE; semi-implementation of hook_wysiwyg_plugin().
316 function wysiwyg_tinymce_plugins($editor) {
319 'path' => $editor['library path'] .
'/themes/advanced',
321 'bold' => t('Bold'), 'italic' => t('Italic'), 'underline' => t('Underline'),
322 'strikethrough' => t('Strike-through'),
323 'justifyleft' => t('Align left'), 'justifycenter' => t('Align center'), 'justifyright' => t('Align right'), 'justifyfull' => t('Justify'),
324 'bullist' => t('Bullet list'), 'numlist' => t('Numbered list'),
325 'outdent' => t('Outdent'), 'indent' => t('Indent'),
326 'undo' => t('Undo'), 'redo' => t('Redo'),
327 'link' => t('Link'), 'unlink' => t('Unlink'), 'anchor' => t('Anchor'),
328 'image' => t('Image'),
329 'cleanup' => t('Clean-up'),
330 'forecolor' => t('Forecolor'), 'backcolor' => t('Backcolor'),
331 'sup' => t('Sup'), 'sub' => t('Sub'),
332 'blockquote' => t('Blockquote'), 'code' => t('Source code'),
333 'hr' => t('Horizontal rule'),
334 'cut' => t('Cut'), 'copy' => t('Copy'), 'paste' => t('Paste'),
335 'visualaid' => t('Visual aid'),
336 'removeformat' => t('Remove format'),
337 'charmap' => t('Character map'),
338 'help' => t('Help')),
342 'path' => $editor['library path'] .
'/plugins/advhr',
343 'buttons' => array('advhr' => t('Advanced horizontal rule')),
344 'extended_valid_elements' => array('hr[class|width|size|noshade]'),
345 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advhr',
350 'path' => $editor['library path'] .
'/plugins/advimage',
351 'extensions' => array('advimage' => t('Advanced image')),
352 'extended_valid_elements' => array('img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]'),
353 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advimage',
358 'path' => $editor['library path'] .
'/plugins/advlink',
359 'extensions' => array('advlink' => t('Advanced link')),
360 'extended_valid_elements' => array('a[name|href|target|title|onclick]'),
361 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlink',
366 'path' => $editor['library path'] .
'/plugins/autosave',
367 'extensions' => array('autosave' => t('Auto save')),
368 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autosave',
372 'contextmenu' => array(
373 'path' => $editor['library path'] .
'/plugins/contextmenu',
374 'extensions' => array('contextmenu' => t('Context menu')),
375 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu',
378 'directionality' => array(
379 'path' => $editor['library path'] .
'/plugins/directionality',
380 'buttons' => array('ltr' => t('Left-to-right'), 'rtl' => t('Right-to-left')),
381 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/directionality',
386 'path' => $editor['library path'] .
'/plugins/emotions',
387 'buttons' => array('emotions' => t('Emotions')),
388 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/emotions',
393 'path' => $editor['library path'] .
'/plugins/flash',
394 'buttons' => array('flash' => t('Flash')),
395 'extended_valid_elements' => array('img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|obj|param|embed]'),
396 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/flash',
401 'path' => $editor['library path'] .
'/plugins/font',
402 'buttons' => array('formatselect' => t('HTML block format'), 'fontselect' => t('Font'), 'fontsizeselect' => t('Font size'), 'styleselect' => t('Font style')),
403 'extended_valid_elements' => array('font[face|size|color|style],span[class|align|style]'),
404 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/font',
407 'fullscreen' => array(
408 'path' => $editor['library path'] .
'/plugins/fullscreen',
409 'buttons' => array('fullscreen' => t('Fullscreen')),
410 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullscreen',
414 'inlinepopups' => array(
415 'path' => $editor['library path'] .
'/plugins/inlinepopups',
416 'extensions' => array('inlinepopups' => t('Inline popups')),
418 'dialog_type' => array('modal'),
420 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/inlinepopups',
424 'insertdatetime' => array(
425 'path' => $editor['library path'] .
'/plugins/insertdatetime',
426 'buttons' => array('insertdate' => t('Insert date'), 'inserttime' => t('Insert time')),
428 'plugin_insertdate_dateFormat' => array('%Y-%m-%d'),
429 'plugin_insertdate_timeFormat' => array('%H:%M:%S'),
431 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/insertdatetime',
436 'path' => $editor['library path'] .
'/plugins/layer',
437 'buttons' => array('insertlayer' => t('Insert layer'), 'moveforward' => t('Move forward'), 'movebackward' => t('Move backward'), 'absolute' => t('Absolute')),
438 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer',
443 'path' => $editor['library path'] .
'/plugins/paste',
444 'buttons' => array('pastetext' => t('Paste text'), 'pasteword' => t('Paste from Word'), 'selectall' => t('Select all')),
445 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',
450 'path' => $editor['library path'] .
'/plugins/preview',
451 'buttons' => array('preview' => t('Preview')),
452 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/preview',
457 'path' => $editor['library path'] .
'/plugins/print',
458 'buttons' => array('print' => t('Print')),
459 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/print',
463 'searchreplace' => array(
464 'path' => $editor['library path'] .
'/plugins/searchreplace',
465 'buttons' => array('search' => t('Search'), 'replace' => t('Replace')),
466 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/searchreplace',
471 'path' => $editor['library path'] .
'/plugins/style',
472 'buttons' => array('styleprops' => t('Style properties')),
473 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/style',
478 'path' => $editor['library path'] .
'/plugins/table',
479 'buttons' => array('tablecontrols' => t('Table')),
480 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/table',
485 if ($editor['installed version'] > 3) {
486 $plugins['safari'] = array(
487 'path' => $editor['library path'] .
'/plugins/safari',
488 'extensions' => array('safari' => t('Safari compatibility')),
489 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari',