annotate modules/filter/filter.api.php @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * Hooks provided by the Filter module.
danielebarchiesi@0 6 */
danielebarchiesi@0 7
danielebarchiesi@0 8 /**
danielebarchiesi@0 9 * @addtogroup hooks
danielebarchiesi@0 10 * @{
danielebarchiesi@0 11 */
danielebarchiesi@0 12
danielebarchiesi@0 13 /**
danielebarchiesi@0 14 * Define content filters.
danielebarchiesi@0 15 *
danielebarchiesi@0 16 * User submitted content is passed through a group of filters before it is
danielebarchiesi@0 17 * output in HTML, in order to remove insecure or unwanted parts, correct or
danielebarchiesi@0 18 * enhance the formatting, transform special keywords, etc. A group of filters
danielebarchiesi@0 19 * is referred to as a "text format". Administrators can create as many text
danielebarchiesi@0 20 * formats as needed. Individual filters can be enabled and configured
danielebarchiesi@0 21 * differently for each text format.
danielebarchiesi@0 22 *
danielebarchiesi@0 23 * This hook is invoked by filter_get_filters() and allows modules to register
danielebarchiesi@0 24 * input filters they provide.
danielebarchiesi@0 25 *
danielebarchiesi@0 26 * Filtering is a two-step process. First, the content is 'prepared' by calling
danielebarchiesi@0 27 * the 'prepare callback' function for every filter. The purpose of the 'prepare
danielebarchiesi@0 28 * callback' is to escape HTML-like structures. For example, imagine a filter
danielebarchiesi@0 29 * which allows the user to paste entire chunks of programming code without
danielebarchiesi@0 30 * requiring manual escaping of special HTML characters like < or &. If the
danielebarchiesi@0 31 * programming code were left untouched, then other filters could think it was
danielebarchiesi@0 32 * HTML and change it. For many filters, the prepare step is not necessary.
danielebarchiesi@0 33 *
danielebarchiesi@0 34 * The second step is the actual processing step. The result from passing the
danielebarchiesi@0 35 * text through all the filters' prepare steps gets passed to all the filters
danielebarchiesi@0 36 * again, this time with the 'process callback' function. The process callbacks
danielebarchiesi@0 37 * should then actually change the content: transform URLs into hyperlinks,
danielebarchiesi@0 38 * convert smileys into images, etc.
danielebarchiesi@0 39 *
danielebarchiesi@0 40 * For performance reasons content is only filtered once; the result is stored
danielebarchiesi@0 41 * in the cache table and retrieved from the cache the next time the same piece
danielebarchiesi@0 42 * of content is displayed. If a filter's output is dynamic, it can override the
danielebarchiesi@0 43 * cache mechanism, but obviously this should be used with caution: having one
danielebarchiesi@0 44 * filter that does not support caching in a particular text format disables
danielebarchiesi@0 45 * caching for the entire format, not just for one filter.
danielebarchiesi@0 46 *
danielebarchiesi@0 47 * Beware of the filter cache when developing your module: it is advised to set
danielebarchiesi@0 48 * your filter to 'cache' => FALSE while developing, but be sure to remove that
danielebarchiesi@0 49 * setting if it's not needed, when you are no longer in development mode.
danielebarchiesi@0 50 *
danielebarchiesi@0 51 * @return
danielebarchiesi@0 52 * An associative array of filters, whose keys are internal filter names,
danielebarchiesi@0 53 * which should be unique and therefore prefixed with the name of the module.
danielebarchiesi@0 54 * Each value is an associative array describing the filter, with the
danielebarchiesi@0 55 * following elements (all are optional except as noted):
danielebarchiesi@0 56 * - title: (required) An administrative summary of what the filter does.
danielebarchiesi@0 57 * - description: Additional administrative information about the filter's
danielebarchiesi@0 58 * behavior, if needed for clarification.
danielebarchiesi@0 59 * - settings callback: The name of a function that returns configuration form
danielebarchiesi@0 60 * elements for the filter. See callback_filter_settings() for details.
danielebarchiesi@0 61 * - default settings: An associative array containing default settings for
danielebarchiesi@0 62 * the filter, to be applied when the filter has not been configured yet.
danielebarchiesi@0 63 * - prepare callback: The name of a function that escapes the content before
danielebarchiesi@0 64 * the actual filtering happens. See callback_filter_prepare() for
danielebarchiesi@0 65 * details.
danielebarchiesi@0 66 * - process callback: (required) The name the function that performs the
danielebarchiesi@0 67 * actual filtering. See callback_filter_process() for details.
danielebarchiesi@0 68 * - cache (default TRUE): Specifies whether the filtered text can be cached.
danielebarchiesi@0 69 * Note that setting this to FALSE makes the entire text format not
danielebarchiesi@0 70 * cacheable, which may have an impact on the site's overall performance.
danielebarchiesi@0 71 * See filter_format_allowcache() for details.
danielebarchiesi@0 72 * - tips callback: The name of a function that returns end-user-facing filter
danielebarchiesi@0 73 * usage guidelines for the filter. See callback_filter_tips() for
danielebarchiesi@0 74 * details.
danielebarchiesi@0 75 * - weight: A default weight for the filter in new text formats.
danielebarchiesi@0 76 *
danielebarchiesi@0 77 * @see filter_example.module
danielebarchiesi@0 78 * @see hook_filter_info_alter()
danielebarchiesi@0 79 */
danielebarchiesi@0 80 function hook_filter_info() {
danielebarchiesi@0 81 $filters['filter_html'] = array(
danielebarchiesi@0 82 'title' => t('Limit allowed HTML tags'),
danielebarchiesi@0 83 'description' => t('Allows you to restrict the HTML tags the user can use. It will also remove harmful content such as JavaScript events, JavaScript URLs and CSS styles from those tags that are not removed.'),
danielebarchiesi@0 84 'process callback' => '_filter_html',
danielebarchiesi@0 85 'settings callback' => '_filter_html_settings',
danielebarchiesi@0 86 'default settings' => array(
danielebarchiesi@0 87 'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>',
danielebarchiesi@0 88 'filter_html_help' => 1,
danielebarchiesi@0 89 'filter_html_nofollow' => 0,
danielebarchiesi@0 90 ),
danielebarchiesi@0 91 'tips callback' => '_filter_html_tips',
danielebarchiesi@0 92 );
danielebarchiesi@0 93 $filters['filter_autop'] = array(
danielebarchiesi@0 94 'title' => t('Convert line breaks'),
danielebarchiesi@0 95 'description' => t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'),
danielebarchiesi@0 96 'process callback' => '_filter_autop',
danielebarchiesi@0 97 'tips callback' => '_filter_autop_tips',
danielebarchiesi@0 98 );
danielebarchiesi@0 99 return $filters;
danielebarchiesi@0 100 }
danielebarchiesi@0 101
danielebarchiesi@0 102 /**
danielebarchiesi@0 103 * Perform alterations on filter definitions.
danielebarchiesi@0 104 *
danielebarchiesi@0 105 * @param $info
danielebarchiesi@0 106 * Array of information on filters exposed by hook_filter_info()
danielebarchiesi@0 107 * implementations.
danielebarchiesi@0 108 */
danielebarchiesi@0 109 function hook_filter_info_alter(&$info) {
danielebarchiesi@0 110 // Replace the PHP evaluator process callback with an improved
danielebarchiesi@0 111 // PHP evaluator provided by a module.
danielebarchiesi@0 112 $info['php_code']['process callback'] = 'my_module_php_evaluator';
danielebarchiesi@0 113
danielebarchiesi@0 114 // Alter the default settings of the URL filter provided by core.
danielebarchiesi@0 115 $info['filter_url']['default settings'] = array(
danielebarchiesi@0 116 'filter_url_length' => 100,
danielebarchiesi@0 117 );
danielebarchiesi@0 118 }
danielebarchiesi@0 119
danielebarchiesi@0 120 /**
danielebarchiesi@0 121 * @} End of "addtogroup hooks".
danielebarchiesi@0 122 */
danielebarchiesi@0 123
danielebarchiesi@0 124 /**
danielebarchiesi@0 125 * Provide a settings form for filter settings.
danielebarchiesi@0 126 *
danielebarchiesi@0 127 * Callback for hook_filter_info().
danielebarchiesi@0 128 *
danielebarchiesi@0 129 * This callback function is used to provide a settings form for filter
danielebarchiesi@0 130 * settings, for filters that need settings on a per-text-format basis. This
danielebarchiesi@0 131 * function should return the form elements for the settings; the filter
danielebarchiesi@0 132 * module will take care of saving the settings in the database.
danielebarchiesi@0 133 *
danielebarchiesi@0 134 * If the filter's behavior depends on an extensive list and/or external data
danielebarchiesi@0 135 * (e.g. a list of smileys, a list of glossary terms), then the filter module
danielebarchiesi@0 136 * can choose to provide a separate, global configuration page rather than
danielebarchiesi@0 137 * per-text-format settings. In that case, the settings callback function
danielebarchiesi@0 138 * should provide a link to the separate settings page.
danielebarchiesi@0 139 *
danielebarchiesi@0 140 * @param $form
danielebarchiesi@0 141 * The prepopulated form array of the filter administration form.
danielebarchiesi@0 142 * @param $form_state
danielebarchiesi@0 143 * The state of the (entire) configuration form.
danielebarchiesi@0 144 * @param $filter
danielebarchiesi@0 145 * The filter object containing the current settings for the given format,
danielebarchiesi@0 146 * in $filter->settings.
danielebarchiesi@0 147 * @param $format
danielebarchiesi@0 148 * The format object being configured.
danielebarchiesi@0 149 * @param $defaults
danielebarchiesi@0 150 * The default settings for the filter, as defined in 'default settings' in
danielebarchiesi@0 151 * hook_filter_info(). These should be combined with $filter->settings to
danielebarchiesi@0 152 * define the form element defaults.
danielebarchiesi@0 153 * @param $filters
danielebarchiesi@0 154 * The complete list of filter objects that are enabled for the given format.
danielebarchiesi@0 155 *
danielebarchiesi@0 156 * @return
danielebarchiesi@0 157 * An array of form elements defining settings for the filter. Array keys
danielebarchiesi@0 158 * should match the array keys in $filter->settings and $defaults.
danielebarchiesi@0 159 *
danielebarchiesi@0 160 * @ingroup callbacks
danielebarchiesi@0 161 */
danielebarchiesi@0 162 function callback_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
danielebarchiesi@0 163 $filter->settings += $defaults;
danielebarchiesi@0 164
danielebarchiesi@0 165 $elements = array();
danielebarchiesi@0 166 $elements['nofollow'] = array(
danielebarchiesi@0 167 '#type' => 'checkbox',
danielebarchiesi@0 168 '#title' => t('Add rel="nofollow" to all links'),
danielebarchiesi@0 169 '#default_value' => $filter->settings['nofollow'],
danielebarchiesi@0 170 );
danielebarchiesi@0 171 return $elements;
danielebarchiesi@0 172 }
danielebarchiesi@0 173
danielebarchiesi@0 174 /**
danielebarchiesi@0 175 * Provide prepared text with special characters escaped.
danielebarchiesi@0 176 *
danielebarchiesi@0 177 * Callback for hook_filter_info().
danielebarchiesi@0 178 *
danielebarchiesi@0 179 * See hook_filter_info() for a description of the filtering process. Filters
danielebarchiesi@0 180 * should not use the 'prepare callback' step for anything other than escaping,
danielebarchiesi@0 181 * because that would short-circuit the control the user has over the order in
danielebarchiesi@0 182 * which filters are applied.
danielebarchiesi@0 183 *
danielebarchiesi@0 184 * @param $text
danielebarchiesi@0 185 * The text string to be filtered.
danielebarchiesi@0 186 * @param $filter
danielebarchiesi@0 187 * The filter object containing settings for the given format.
danielebarchiesi@0 188 * @param $format
danielebarchiesi@0 189 * The text format object assigned to the text to be filtered.
danielebarchiesi@0 190 * @param $langcode
danielebarchiesi@0 191 * The language code of the text to be filtered.
danielebarchiesi@0 192 * @param $cache
danielebarchiesi@0 193 * A Boolean indicating whether the filtered text is going to be cached in
danielebarchiesi@0 194 * {cache_filter}.
danielebarchiesi@0 195 * @param $cache_id
danielebarchiesi@0 196 * The ID of the filtered text in {cache_filter}, if $cache is TRUE.
danielebarchiesi@0 197 *
danielebarchiesi@0 198 * @return
danielebarchiesi@0 199 * The prepared, escaped text.
danielebarchiesi@0 200 *
danielebarchiesi@0 201 * @ingroup callbacks
danielebarchiesi@0 202 */
danielebarchiesi@0 203 function callback_filter_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
danielebarchiesi@0 204 // Escape <code> and </code> tags.
danielebarchiesi@0 205 $text = preg_replace('|<code>(.+?)</code>|se', "[codefilter_code]$1[/codefilter_code]", $text);
danielebarchiesi@0 206 return $text;
danielebarchiesi@0 207 }
danielebarchiesi@0 208
danielebarchiesi@0 209 /**
danielebarchiesi@0 210 * Provide text filtered to conform to the supplied format.
danielebarchiesi@0 211 *
danielebarchiesi@0 212 * Callback for hook_filter_info().
danielebarchiesi@0 213 *
danielebarchiesi@0 214 * See hook_filter_info() for a description of the filtering process. This step
danielebarchiesi@0 215 * is where the filter actually transforms the text.
danielebarchiesi@0 216 *
danielebarchiesi@0 217 * @param $text
danielebarchiesi@0 218 * The text string to be filtered.
danielebarchiesi@0 219 * @param $filter
danielebarchiesi@0 220 * The filter object containing settings for the given format.
danielebarchiesi@0 221 * @param $format
danielebarchiesi@0 222 * The text format object assigned to the text to be filtered.
danielebarchiesi@0 223 * @param $langcode
danielebarchiesi@0 224 * The language code of the text to be filtered.
danielebarchiesi@0 225 * @param $cache
danielebarchiesi@0 226 * A Boolean indicating whether the filtered text is going to be cached in
danielebarchiesi@0 227 * {cache_filter}.
danielebarchiesi@0 228 * @param $cache_id
danielebarchiesi@0 229 * The ID of the filtered text in {cache_filter}, if $cache is TRUE.
danielebarchiesi@0 230 *
danielebarchiesi@0 231 * @return
danielebarchiesi@0 232 * The filtered text.
danielebarchiesi@0 233 *
danielebarchiesi@0 234 * @ingroup callbacks
danielebarchiesi@0 235 */
danielebarchiesi@0 236 function callback_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
danielebarchiesi@0 237 $text = preg_replace('|\[codefilter_code\](.+?)\[/codefilter_code\]|se', "<pre>$1</pre>", $text);
danielebarchiesi@0 238
danielebarchiesi@0 239 return $text;
danielebarchiesi@0 240 }
danielebarchiesi@0 241
danielebarchiesi@0 242 /**
danielebarchiesi@0 243 * Return help text for a filter.
danielebarchiesi@0 244 *
danielebarchiesi@0 245 * Callback for hook_filter_info().
danielebarchiesi@0 246 *
danielebarchiesi@0 247 * A filter's tips should be informative and to the point. Short tips are
danielebarchiesi@0 248 * preferably one-liners.
danielebarchiesi@0 249 *
danielebarchiesi@0 250 * @param $filter
danielebarchiesi@0 251 * An object representing the filter.
danielebarchiesi@0 252 * @param $format
danielebarchiesi@0 253 * An object representing the text format the filter is contained in.
danielebarchiesi@0 254 * @param $long
danielebarchiesi@0 255 * Whether this callback should return a short tip to display in a form
danielebarchiesi@0 256 * (FALSE), or whether a more elaborate filter tips should be returned for
danielebarchiesi@0 257 * theme_filter_tips() (TRUE).
danielebarchiesi@0 258 *
danielebarchiesi@0 259 * @return
danielebarchiesi@0 260 * Translated text to display as a tip.
danielebarchiesi@0 261 *
danielebarchiesi@0 262 * @ingroup callbacks
danielebarchiesi@0 263 */
danielebarchiesi@0 264 function callback_filter_tips($filter, $format, $long) {
danielebarchiesi@0 265 if ($long) {
danielebarchiesi@0 266 return t('Lines and paragraphs are automatically recognized. The &lt;br /&gt; line break, &lt;p&gt; paragraph and &lt;/p&gt; close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.');
danielebarchiesi@0 267 }
danielebarchiesi@0 268 else {
danielebarchiesi@0 269 return t('Lines and paragraphs break automatically.');
danielebarchiesi@0 270 }
danielebarchiesi@0 271 }
danielebarchiesi@0 272
danielebarchiesi@0 273 /**
danielebarchiesi@0 274 * @addtogroup hooks
danielebarchiesi@0 275 * @{
danielebarchiesi@0 276 */
danielebarchiesi@0 277
danielebarchiesi@0 278 /**
danielebarchiesi@0 279 * Perform actions when a new text format has been created.
danielebarchiesi@0 280 *
danielebarchiesi@0 281 * @param $format
danielebarchiesi@0 282 * The format object of the format being updated.
danielebarchiesi@0 283 *
danielebarchiesi@0 284 * @see hook_filter_format_update()
danielebarchiesi@0 285 * @see hook_filter_format_disable()
danielebarchiesi@0 286 */
danielebarchiesi@0 287 function hook_filter_format_insert($format) {
danielebarchiesi@0 288 mymodule_cache_rebuild();
danielebarchiesi@0 289 }
danielebarchiesi@0 290
danielebarchiesi@0 291 /**
danielebarchiesi@0 292 * Perform actions when a text format has been updated.
danielebarchiesi@0 293 *
danielebarchiesi@0 294 * This hook allows modules to act when a text format has been updated in any
danielebarchiesi@0 295 * way. For example, when filters have been reconfigured, disabled, or
danielebarchiesi@0 296 * re-arranged in the text format.
danielebarchiesi@0 297 *
danielebarchiesi@0 298 * @param $format
danielebarchiesi@0 299 * The format object of the format being updated.
danielebarchiesi@0 300 *
danielebarchiesi@0 301 * @see hook_filter_format_insert()
danielebarchiesi@0 302 * @see hook_filter_format_disable()
danielebarchiesi@0 303 */
danielebarchiesi@0 304 function hook_filter_format_update($format) {
danielebarchiesi@0 305 mymodule_cache_rebuild();
danielebarchiesi@0 306 }
danielebarchiesi@0 307
danielebarchiesi@0 308 /**
danielebarchiesi@0 309 * Perform actions when a text format has been disabled.
danielebarchiesi@0 310 *
danielebarchiesi@0 311 * @param $format
danielebarchiesi@0 312 * The format object of the format being disabled.
danielebarchiesi@0 313 *
danielebarchiesi@0 314 * @see hook_filter_format_insert()
danielebarchiesi@0 315 * @see hook_filter_format_update()
danielebarchiesi@0 316 */
danielebarchiesi@0 317 function hook_filter_format_disable($format) {
danielebarchiesi@0 318 mymodule_cache_rebuild();
danielebarchiesi@0 319 }
danielebarchiesi@0 320
danielebarchiesi@0 321 /**
danielebarchiesi@0 322 * @} End of "addtogroup hooks".
danielebarchiesi@0 323 */