Same name and namespace in other branches
- 4.7.x developer/examples/filter_example.module \filter_example_filter()
- 5.x developer/examples/filter_example.module \filter_example_filter()
Implementation of hook_filter().
The bulk of filtering work is done here. This hook is quite complicated, so we'll discuss each operation it defines.
File
- developer/
examples/ filter_example.module, line 63 - This is an example outlining how a module can be used to define a filter to be run on user-submitted content before it is output to the browser.
Code
function filter_example_filter($op, $delta = 0, $format = -1, $text = '') {
// The "list" operation provides the module an opportunity to declare both how
// many filters it defines and a human-readable name for each filter. Note that
// the returned name should be passed through t() for translation.
if ($op == 'list') {
return array(
0 => t('Substitute "foo"'),
1 => t('Current time'),
);
}
// All operations besides "list" provide a $delta argument so we know which
// filter they refer to. We'll switch on that argument now so that we can
// discuss each filter in turn.
switch ($delta) {
// First we define the simple string substitution filter.
case 0:
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Substitutes a custom string for the string "foo" in the text.');
// We don't need the "prepare" operation for this filter, but it's required
// to at least return the input text as-is.
case 'prepare':
return $text;
// The actual filtering is performed here. The supplied text should be
// returned, once any necessary substitutions have taken place.
case 'process':
return str_replace('foo', variable_get('filter_example_foo_' . $format, 'bar'), $text);
// Since we allow the administrator to define the string that gets
// substituted when "foo" is encountered, we need to provide an interface
// for this customization. Note that the value of $format needs to be
// provided as part of the form name, so that different customization can
// be done for this filter in each of the different input formats that may
// use it.
case 'settings':
return form_textfield(t('Substitution string'), 'filter_example_foo_' . $format, variable_get('filter_example_foo_' . $format, 'bar'), 30, 255, t('The string to substitute for "foo" everywhere in the text.'));
}
break;
// Next is our "time tag" filter.
case 1:
switch ($op) {
// This description is shown in the administrative interface, unlike the
// filter tips which are shown in the content editing interface.
case 'description':
return t('Inserts the current time in the place of <time /> tags.');
// Since this filter will return a different result on each page load, we
// need to return TRUE for "no cache" to ensure that the filter is run
// every time the text is requested.
case 'no cache':
return TRUE;
// This filter is a little trickier to implement than the previous one.
// Since the input involves special HTML characters (< and >) we have to
// run the filter before HTML is escaped/stripped by other filters. But
// we want to use HTML in our result as well, and so if we run this filter
// first our replacement string could be escaped or stripped. The solution
// is to use the "prepare" operation to escape the special characters, and
// to later replace our escaped version in the "process" step.
//
// We'll use the bytes 0xFE and 0xFF to replace < and > here. These bytes
// are not valid in UTF-8 data and thus unlikely to cause problems.
case 'prepare':
return preg_replace('!<time ?/>!', '\\xFEtime /\\xFF', $text);
// Now, in the "process" step, we'll search for our escaped time tags and
// to the real filtering.
case 'process':
return str_replace('\\xFEtime /\\xFF', '<em>' . format_date(time()) . '</em>', $text);
}
break;
}
}