hook_filter_info()Define content filters.
Content in Drupal is passed through a group of filters before it is output. This lets a module modify content to the site administrator's liking.
This hook allows modules to declare input filters they provide. A module can contain as many filters as it wants.
The overall, logical flow is as follows:
Additionally, if a filter is configurable:
Filtering is a two-step process. First, the content is 'prepared' by calling the 'prepare callback' function for every filter. The purpose of the 'prepare callback' is to escape HTML-like structures. For example, imagine a filter which allows the user to paste entire chunks of programming code without requiring manual escaping of special HTML characters like @< or @&. If the programming code were left untouched, then other filters could think it was HTML and change it. For most filters however, the prepare-step is not necessary, and they can just return the input without changes.
Filters should not use the 'prepare callback' step for anything other than escaping, because that would short-circuit the control the user has over the order in which filters are applied.
The second step is the actual processing step. The result from the prepare step gets passed to all the filters again, this time with the 'process callback' function. It's here where filters should perform actual changing of the content: transforming URLs into hyperlinks, converting smileys into images, etc.
An important aspect of the filtering system is 'text formats'. Every text format is an entire filter setup: which filters to enable, in what order and with what settings.
Filters that require settings should provide the form controls to configure the settings in a form builder function, specified in 'settings callback'. The filter system stores the settings in the database per text format.
If the filter's behavior depends on an extensive list and/or external data (e.g. a list of smileys, a list of glossary terms) then filters are allowed to provide a separate, global configuration page rather than provide settings per format. In that case, there should be a link from the format-specific settings to the separate settings page.
The $filter object with the current settings is passed to the 'settings callback' function. If 'default settings' were defined in hook_filter_info(), those are passed as second argument to the 'settings callback'. Each filter should apply either the default settings or the configured settings contained in $filter->settings.
'settings callback' is invoked with the following arguments (most filter implementations will only need $form_state, $filter and $defaults):
<?php
function mymodule_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
$settings['mymodule_url_length'] = array(
'#type' => 'textfield',
'#title' => t('Maximum link text length'),
'#default_value' => isset($filter->settings['mymodule_url_length']) ? $filter->settings['mymodule_url_length'] : $defaults['mymodule_url_length'],
);
return $settings;
}
?>'prepare callback' and 'process callback' are invoked with the following arguments:
'prepare callback' and 'process callback' functions may access the filter settings in $filter->settings['mymodule_url_length'].
'tips callback' is invoked with the following parameters:
For performance reasons content is only filtered once; the result is stored in the cache table and retrieved from the cache the next time the same piece of content is displayed. If a filter's output is dynamic, it can override the cache mechanism, but obviously this should be used with caution: having one filter that does not support caching in a particular text format disables caching for the entire format, not just for one filter.
Beware of the filter cache when developing your module: it is advised to set your filter to 'cache' => FALSE while developing, but be sure to remove it again if it's not needed. You can clear the cache by running the SQL query 'DELETE * FROM cache_filter';
For a detailed usage example, see filter_example.module. For an example of using multiple filters in one module, see filter_filter_info().
An array of filter items. Each filter item has a unique name, prefixed with the name of the module that provides it. The item is an associative array that may contain the following key-value pairs:
modules/filter/filter.api.php, line 164
<?php
function hook_filter_info() {
$filters['filter_html'] = array(
'title' => t('Limit allowed HTML tags'),
'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.'),
'process callback' => '_filter_html',
'settings callback' => '_filter_html_settings',
'default settings' => array(
'allowed_html' => '<a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>',
'filter_html_help' => 1,
'filter_html_nofollow' => 0,
),
'tips callback' => '_filter_html_tips',
);
$filters['filter_autop'] = array(
'title' => t('Convert line breaks'),
'description' => t('Converts line breaks into HTML (i.e. <br> and <p>) tags.'),
'process callback' => '_filter_autop',
'tips callback' => '_filter_autop_tips',
);
return $filters;
}
?>