5 * Hooks provided by the Filter module.
14 * Define content filters.
16 * User submitted content is passed through a group of filters before it is
17 * output in HTML, in order to remove insecure or unwanted parts, correct or
18 * enhance the formatting, transform special keywords, etc. A group of filters
19 * is referred to as a "text format". Administrators can create as many text
20 * formats as needed. Individual filters can be enabled and configured
21 * differently for each text format.
23 * This hook is invoked by filter_get_filters() and allows modules to register
24 * input filters they provide.
26 * Filtering is a two-step process. First, the content is 'prepared' by calling
27 * the 'prepare callback' function for every filter. The purpose of the 'prepare
28 * callback' is to escape HTML-like structures. For example, imagine a filter
29 * which allows the user to paste entire chunks of programming code without
30 * requiring manual escaping of special HTML characters like < or &. If the
31 * programming code were left untouched, then other filters could think it was
32 * HTML and change it. For many filters, the prepare step is not necessary.
34 * The second step is the actual processing step. The result from passing the
35 * text through all the filters' prepare steps gets passed to all the filters
36 * again, this time with the 'process callback' function. The process callbacks
37 * should then actually change the content: transform URLs into hyperlinks,
38 * convert smileys into images, etc.
40 * For performance reasons content is only filtered once; the result is stored
41 * in the cache table and retrieved from the cache the next time the same piece
42 * of content is displayed. If a filter's output is dynamic, it can override the
43 * cache mechanism, but obviously this should be used with caution: having one
44 * filter that does not support caching in a particular text format disables
45 * caching for the entire format, not just for one filter.
47 * Beware of the filter cache when developing your module: it is advised to set
48 * your filter to 'cache' => FALSE while developing, but be sure to remove that
49 * setting if it's not needed, when you are no longer in development mode.
52 * An associative array of filters, whose keys are internal filter names,
53 * which should be unique and therefore prefixed with the name of the module.
54 * Each value is an associative array describing the filter, with the
55 * following elements (all are optional except as noted):
56 * - title: (required) An administrative summary of what the filter does.
57 * - description: Additional administrative information about the filter's
58 * behavior, if needed for clarification.
59 * - settings callback: The name of a function that returns configuration form
60 * elements for the filter. See callback_filter_settings() for details.
61 * - default settings: An associative array containing default settings for
62 * the filter, to be applied when the filter has not been configured yet.
63 * - prepare callback: The name of a function that escapes the content before
64 * the actual filtering happens. See callback_filter_prepare() for
66 * - process callback: (required) The name the function that performs the
67 * actual filtering. See callback_filter_process() for details.
68 * - cache (default TRUE): Specifies whether the filtered text can be cached.
69 * Note that setting this to FALSE makes the entire text format not
70 * cacheable, which may have an impact on the site's overall performance.
71 * See filter_format_allowcache() for details.
72 * - tips callback: The name of a function that returns end-user-facing filter
73 * usage guidelines for the filter. See callback_filter_tips() for
75 * - weight: A default weight for the filter in new text formats.
77 * @see filter_example.module
78 * @see hook_filter_info_alter()
80 function hook_filter_info() {
81 $filters['filter_html'] = array(
82 'title' => t('Limit allowed HTML tags'),
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.'),
84 'process callback' => '_filter_html',
85 'settings callback' => '_filter_html_settings',
86 'default settings' => array(
87 'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>',
88 'filter_html_help' => 1,
89 'filter_html_nofollow' => 0,
91 'tips callback' => '_filter_html_tips',
93 $filters['filter_autop'] = array(
94 'title' => t('Convert line breaks'),
95 'description' => t('Converts line breaks into HTML (i.e. <br> and <p>) tags.'),
96 'process callback' => '_filter_autop',
97 'tips callback' => '_filter_autop_tips',
103 * Perform alterations on filter definitions.
106 * Array of information on filters exposed by hook_filter_info()
109 function hook_filter_info_alter(&$info) {
110 // Replace the PHP evaluator process callback with an improved
111 // PHP evaluator provided by a module.
112 $info['php_code']['process callback'] = 'my_module_php_evaluator';
114 // Alter the default settings of the URL filter provided by core.
115 $info['filter_url']['default settings'] = array(
116 'filter_url_length' => 100,
121 * @} End of "addtogroup hooks".
125 * Provide a settings form for filter settings.
127 * Callback for hook_filter_info().
129 * This callback function is used to provide a settings form for filter
130 * settings, for filters that need settings on a per-text-format basis. This
131 * function should return the form elements for the settings; the filter
132 * module will take care of saving the settings in the database.
134 * If the filter's behavior depends on an extensive list and/or external data
135 * (e.g. a list of smileys, a list of glossary terms), then the filter module
136 * can choose to provide a separate, global configuration page rather than
137 * per-text-format settings. In that case, the settings callback function
138 * should provide a link to the separate settings page.
141 * The prepopulated form array of the filter administration form.
143 * The state of the (entire) configuration form.
145 * The filter object containing the current settings for the given format,
146 * in $filter->settings.
148 * The format object being configured.
150 * The default settings for the filter, as defined in 'default settings' in
151 * hook_filter_info(). These should be combined with $filter->settings to
152 * define the form element defaults.
154 * The complete list of filter objects that are enabled for the given format.
157 * An array of form elements defining settings for the filter. Array keys
158 * should match the array keys in $filter->settings and $defaults.
162 function callback_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
163 $filter->settings +
= $defaults;
166 $elements['nofollow'] = array(
167 '#type' => 'checkbox',
168 '#title' => t('Add rel="nofollow" to all links'),
169 '#default_value' => $filter->settings
['nofollow'],
175 * Provide prepared text with special characters escaped.
177 * Callback for hook_filter_info().
179 * See hook_filter_info() for a description of the filtering process. Filters
180 * should not use the 'prepare callback' step for anything other than escaping,
181 * because that would short-circuit the control the user has over the order in
182 * which filters are applied.
185 * The text string to be filtered.
187 * The filter object containing settings for the given format.
189 * The text format object assigned to the text to be filtered.
191 * The language code of the text to be filtered.
193 * A Boolean indicating whether the filtered text is going to be cached in
196 * The ID of the filtered text in {cache_filter}, if $cache is TRUE.
199 * The prepared, escaped text.
203 function callback_filter_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
204 // Escape <code> and </code> tags.
205 $text = preg_replace('|<code>(.+?)</code>|se', "[codefilter_code]$1[/codefilter_code]", $text);
210 * Provide text filtered to conform to the supplied format.
212 * Callback for hook_filter_info().
214 * See hook_filter_info() for a description of the filtering process. This step
215 * is where the filter actually transforms the text.
218 * The text string to be filtered.
220 * The filter object containing settings for the given format.
222 * The text format object assigned to the text to be filtered.
224 * The language code of the text to be filtered.
226 * A Boolean indicating whether the filtered text is going to be cached in
229 * The ID of the filtered text in {cache_filter}, if $cache is TRUE.
236 function callback_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
237 $text = preg_replace('|\[codefilter_code\](.+?)\[/codefilter_code\]|se', "<pre>$1</pre>", $text);
243 * Return help text for a filter.
245 * Callback for hook_filter_info().
247 * A filter's tips should be informative and to the point. Short tips are
248 * preferably one-liners.
251 * An object representing the filter.
253 * An object representing the text format the filter is contained in.
255 * Whether this callback should return a short tip to display in a form
256 * (FALSE), or whether a more elaborate filter tips should be returned for
257 * theme_filter_tips() (TRUE).
260 * Translated text to display as a tip.
264 function callback_filter_tips($filter, $format, $long) {
266 return t('Lines and paragraphs are automatically recognized. The <br /> line break, <p> paragraph and </p> close paragraph tags are inserted automatically. If paragraphs are not recognized simply add a couple blank lines.');
269 return t('Lines and paragraphs break automatically.');
279 * Perform actions when a new text format has been created.
282 * The format object of the format being updated.
284 * @see hook_filter_format_update()
285 * @see hook_filter_format_disable()
287 function hook_filter_format_insert($format) {
288 mymodule_cache_rebuild();
292 * Perform actions when a text format has been updated.
294 * This hook allows modules to act when a text format has been updated in any
295 * way. For example, when filters have been reconfigured, disabled, or
296 * re-arranged in the text format.
299 * The format object of the format being updated.
301 * @see hook_filter_format_insert()
302 * @see hook_filter_format_disable()
304 function hook_filter_format_update($format) {
305 mymodule_cache_rebuild();
309 * Perform actions when a text format has been disabled.
312 * The format object of the format being disabled.
314 * @see hook_filter_format_insert()
315 * @see hook_filter_format_update()
317 function hook_filter_format_disable($format) {
318 mymodule_cache_rebuild();
322 * @} End of "addtogroup hooks".