editor_ckeditor.api.php 6.06 KB
Newer Older
Devin Carlson's avatar
Devin Carlson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<?php

/**
 * @file
 * Documentation for Editor CKEditor module APIs.
 */

/**
 * @addtogroup hooks
 * @{
 */
/**
 * Provides a list of CKEditor plugins.
 *
 * Each plugin for CKEditor must provide an array of properties containing
 * information about the plugin. At minimum, plugins must provide a path and
 * file location so that CKEditor may add the plugin. Available properties for
 * each plugin include:
 *
 * - location: Required for all external plugins. String path to the plugin
 *   directory relative to the Drupal installation root. Do not include a
 *   trailing slash.
 * - file: Required for all external plugins. String file name of the plugin in
 *   the "location" directory.
 * - internal: Boolean value indicating if the plugin is part of the compressed
 *   CKEditor library package and already loaded on all instances. If TRUE,
 *   the "location" and "file" properties are not needed.
 * - css: An array of CSS files that should be added by CKEditor. These files
 *   are used only when CKEditor is using an iframe wrapper around its content.
 *   If a plugin needs to include CSS for inline and iframe versions, it should
 *   add its CSS via CKEditor's JavaScript CKEDITOR.addCss() method.
 * - enabled callback: String containing a function name that can determine if
 *   this plugin should be enabled based on the current editor configuration.
Devin Carlson's avatar
Devin Carlson committed
34
 *   See the hook_editor_ckeditor_PLUGIN_plugin_check() function for an example.
Devin Carlson's avatar
Devin Carlson committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * - buttons: An array of buttons that are provided by this plugin. Each button
 *   should by keyed by its CKEditor button name, and should contain an array
 *   of button properties, including:
 *   - label: A human-readable, translated button name.
 *   - image: An image for the button to be used in the toolbar.
 *   - image_rtl: If the image needs to have a right-to-left version, specify
 *     an alternative file that will be used in RTL editors.
 *   - image_alternative: If this button does not render as an image, specify
 *     an HTML string representing the contents of this button. This alternative
 *     will only be used in the administrative section for assembling the
 *     toolbar.
 *   - attributes: An array of HTML attributes which should be added to this
 *     button when rendering the button in the administrative section for
 *     assembling the toolbar.
 *   - multiple: Boolean value indicating if this button may be added multiple
 *     times to the toolbar. This typically is only applicable for dividers and
 *     group indicators.
 *   - required_html: If this button requires certain HTML tags to be allowed,
 *     specify an array of tags.
54
 *
Devin Carlson's avatar
Devin Carlson committed
55 56 57
 * @return array
 *   An array of plugin definitions, keyed by the plugin name.
 *
Devin Carlson's avatar
Devin Carlson committed
58 59 60
 * @see editor_ckeditor_editor_ckeditor_plugins()
 * @see editor_ckeditor_editor_ckeditor_plugins_alter()
 * @see hook_editor_ckeditor_PLUGIN_plugin_check()
Devin Carlson's avatar
Devin Carlson committed
61 62 63 64 65 66 67 68 69 70
 */
function hook_editor_ckeditor_plugins() {
  $plugins['myplugin'] = array(
    'path' => drupal_get_path('module', 'mymodule') . '/js/myplugin',
    'file' => 'plugin.js',
    'css' => array(drupal_get_path('module', 'mymodule') . '/css/myplugin.css'),
    'enabled callback' => 'mymodule_myplugin_plugin_check',
  );
  return $plugins;
}
Devin Carlson's avatar
Devin Carlson committed
71

Devin Carlson's avatar
Devin Carlson committed
72 73 74 75 76 77
/**
 * Modify the list of available CKEditor plugins.
 *
 * This hook may be used to modify plugin properties after they have been
 * specified by other modules.
 *
78
 * @param array $plugins
Devin Carlson's avatar
Devin Carlson committed
79 80
 *   An array of all the existing plugin definitions, passed by reference.
 *
Devin Carlson's avatar
Devin Carlson committed
81
 * @see hook_editor_ckeditor_plugins()
Devin Carlson's avatar
Devin Carlson committed
82 83 84 85
 */
function hook_editor_ckeditor_plugins_alter(array &$plugins) {
  $plugins['someplugin']['enabled callback'] = 'mymodule_someplugin_enabled_callback';
}
Devin Carlson's avatar
Devin Carlson committed
86

Devin Carlson's avatar
Devin Carlson committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100
/**
 * Modify the list of CSS files that will be added to a CKEditor instance.
 *
 * Modules may use this hook to provide their own custom CSS file without
 * providing a CKEditor plugin. This list of CSS files is only used in the
 * iframe versions of CKEditor.
 *
 * Note that because this hook is only called for modules and the active theme,
 * front-end themes will not be able to use this hook to add their own CSS files
 * if a different admin theme is active. Instead, front-end themes and base
 * themes may specify CSS files to be used in iframe instances of CKEditor
 * through an entry in their .info file:
 *
 * @code
101
 * editor_ckeditor_stylesheets[] = css/ckeditor-iframe.css
Devin Carlson's avatar
Devin Carlson committed
102 103
 * @endcode
 *
104
 * @param array $css
Devin Carlson's avatar
Devin Carlson committed
105 106
 *   An array of CSS files, passed by reference. This is a flat list of file
 *   paths relative to the Drupal root.
107
 * @param object $format
Devin Carlson's avatar
Devin Carlson committed
108 109 110 111 112
 *   The corresponding text format object as returned by filter_format_load()
 *   for which the current text editor is being displayed.
 *
 * @see _ckeditor_theme_css()
 */
Devin Carlson's avatar
Devin Carlson committed
113
function hook_editor_ckeditor_css_alter(array &$css, $format) {
Devin Carlson's avatar
Devin Carlson committed
114 115 116 117 118 119 120 121 122
  $css[] = drupal_get_path('module', 'mymodule') . '/css/mymodule-ckeditor.css';
}
/**
 * @} End of "addtogroup hooks".
 */
/**
 * Enabled callback for hook_ckeditor_plugins().
 *
 * Note: This is not really a hook. The function name is manually specified via
Devin Carlson's avatar
Devin Carlson committed
123 124
 * 'enabled callback' in hook_editor_ckeditor_plugins(), with this recommended
 * callback name pattern. It is called from editor_ckeditor_get_settings().
Devin Carlson's avatar
Devin Carlson committed
125 126 127 128 129 130 131 132 133 134 135 136
 *
 * This callback should determine if a plugin should be enabled for a CKEditor
 * instance. Plugins may be enabled based off an explicit setting, or enable
 * themselves based on the configuration of another setting, such as enabling
 * based on a particular button being present in the toolbar.
 *
 * @param object $format
 *   An format object as returned by filter_format_load(). The editor's settings
 *   may be found in $format->editor_settings.
 * @param string $plugin_name
 *   String name of the plugin that is being checked.
 *
137
 * @return bool
Devin Carlson's avatar
Devin Carlson committed
138 139
 *   Boolean TRUE if the plugin should be enabled, FALSE otherwise.
 *
Devin Carlson's avatar
Devin Carlson committed
140 141
 * @see hook_editor_ckeditor_plugins()
 * @see editor_ckeditor_get_settings()
Devin Carlson's avatar
Devin Carlson committed
142 143 144 145 146 147 148 149 150
 */
function hook_editor_ckeditor_PLUGIN_plugin_check($format, $plugin_name) {
  // Automatically enable this plugin if the Underline button is enabled.
  foreach ($format->editor_settings['toolbar']['buttons'] as $row) {
    if (in_array('Underline', $row)) {
      return TRUE;
    }
  }
}