This example demonstrates how filters are created.
This is an example outlining how a module can define a filter that runs on user-submitted content before it is output on the browser.
To show all the capabilities of the filter system, we will define two filters in this module. One will substitute the string "foo" with an administratively-defined replacement string; the other one will replace a XML tag, <time />, with the current time.
Foo filter
Backdrop has several content formats (they are not filters). In our example the foo replacement can be configured for each one of them, allowing a HTML or PHP replacement. The module includes a settings callback, with options to configure that replacements. A tips callback will show the current replacement for the node that is edited.
Time filter.
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 the HTML markup is escaped/stripped by other filters. We want to use HTML in our result as well; 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.
Parent topics
File
- modules/
examples/ filter_example/ filter_example.module, line 7 - Hook implementations for the Filter Example module.
Functions
Name | Location | Description |
---|---|---|
filter_example_filter_info |
modules/ |
Implements hook_filter_info(). |
filter_example_menu |
modules/ |
Implements hook_menu(). |
_filter_example_filter_foo_process |
modules/ |
Foo filter process callback. |
_filter_example_filter_foo_settings |
modules/ |
Settings callback for foo filter. |
_filter_example_filter_foo_tips |
modules/ |
Filter tips callback for foo filter. |
_filter_example_filter_time_prepare |
modules/ |
Time filter prepare callback. |
_filter_example_filter_time_process |
modules/ |
Time filter process callback. |
_filter_example_filter_time_tips |
modules/ |
Filter tips callback for time filter. |
_filter_example_information |
modules/ |
Simply returns a little bit of information about the example. |