filter_example.module

  1. 7 filter_example/filter_example.module
  2. 6 filter_example/filter_example.module

Module file for filter_example.

Functions

Namesort descending Description
filter_example_filter_info Implements hook_filter_info().
filter_example_help Implements hook_help().
filter_example_menu Implements hook_menu().
_filter_example_filter_foo_process Foo filter process callback.
_filter_example_filter_foo_settings Settings callback for foo filter
_filter_example_filter_foo_tips Filter tips callback for foo filter.
_filter_example_filter_time_prepare Time filter prepare callback.
_filter_example_filter_time_process Time filter process callback.
_filter_example_filter_time_tips Filter tips callback for time filter.
_filter_example_information Simply returns a little bit of information about the example.

File

filter_example/filter_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Module file for filter_example.
  5. */
  6. /**
  7. * @defgroup filter_example Example: Filter
  8. * @ingroup examples
  9. * @{
  10. * Demonstrates the creation of filters.
  11. * This is an example outlining how a module can be used to define a filter
  12. * to be run on user-submitted content before it is output to the browser.
  13. *
  14. * To show all the capabilities of the filter system, we will define two filters
  15. * in this module. One will substitute the string "foo" with an
  16. * administratively-defined replacement string. The other will find a custom
  17. * XML tag, <time />, and replace it by the current time.
  18. *
  19. * Foo filter
  20. *
  21. * Drupal has several content formats (they are not filters), and in our example
  22. * the foo replacement can be configured for each one of them, allowing an html
  23. * or php replacement, so the module includes a settings callback, with options
  24. * to configure that replacements. Also, a Tips callback will help showing the
  25. * current replacement for the content type being edited.
  26. *
  27. * Time filter.
  28. *
  29. * This filter is a little trickier to implement than the previous one.
  30. * Since the input involves special HTML characters (< and >) we have to
  31. * run the filter before HTML is escaped/stripped by other filters. But
  32. * we want to use HTML in our result as well, and so if we run this filter
  33. * first our replacement string could be escaped or stripped. The solution
  34. * is to use the "prepare" operation to escape the special characters, and
  35. * to later replace our escaped version in the "process" step.
  36. */
  37. /**
  38. * Implements hook_menu().
  39. */
  40. function filter_example_menu() {
  41. $items['examples/filter_example'] = array(
  42. 'title' => 'Filter Example',
  43. 'page callback' => '_filter_example_information',
  44. 'access callback' => TRUE,
  45. );
  46. return $items;
  47. }
  48. /**
  49. * Implements hook_help().
  50. */
  51. function filter_example_help($path, $arg) {
  52. switch ($path) {
  53. case 'admin/help#filter_example':
  54. return _filter_example_information();
  55. }
  56. }
  57. /**
  58. * Simply returns a little bit of information about the example.
  59. */
  60. function _filter_example_information() {
  61. return t("<p>This example provides two filters.</p><p>Foo Filter replaces
  62. 'foo' with a configurable replacement.</p><p>Time Tag replaces the string
  63. '&lt;time /&gt;' with the current time.</p><p>To use these filters, go to !link and
  64. configure an input format, or create a new one.</p>",
  65. array('!link' => l("admin/config/content/formats", "admin/config/content/formats"))
  66. );
  67. }
  68. /**
  69. * Implements hook_filter_info().
  70. *
  71. * Here we define the different filters provided by the module. For this example,
  72. * time_filter is a very static and simple replacement, but it requires some
  73. * preparation of the string because of the special html tags < and >. The
  74. * foo_filter is more complex, including its own settings and inline tips.
  75. */
  76. function filter_example_filter_info() {
  77. $filters['filter_foo'] = array(
  78. 'title' => t('Foo Filter (example)'),
  79. 'description' => t('Every instance of "foo" in the input text will be replaced with a preconfigured replacement.'),
  80. 'process callback' => '_filter_example_filter_foo_process',
  81. 'default settings' => array(
  82. 'filter_example_foo' => 'bar',
  83. ),
  84. 'settings callback' => '_filter_example_filter_foo_settings',
  85. 'tips callback' => '_filter_example_filter_foo_tips',
  86. );
  87. $filters['filter_time'] = array(
  88. 'title' => t('Time Tag (example)'),
  89. 'description' => t('Every instance of the special &lt;time /&gt; tag will be replaced with the current date and time in the user\'s specified time zone.'),
  90. 'prepare callback' => '_filter_example_filter_time_prepare',
  91. 'process callback' => '_filter_example_filter_time_process',
  92. 'tips callback' => '_filter_example_filter_time_tips',
  93. );
  94. return $filters;
  95. }
  96. /*
  97. * Foo filter
  98. *
  99. * Drupal has several text formats (they are not filters), and in our example
  100. * the foo replacement can be configured for each one of them, so the module
  101. * includes a settings callback, with options to configure those replacements.
  102. * Also, a Tips callback will help showing the current replacement
  103. * for the content type being edited.
  104. */
  105. /**
  106. * Settings callback for foo filter
  107. *
  108. * Make use of $format to have different replacements for every input format.
  109. * Since we allow the administrator to define the string that gets substituted
  110. * when "foo" is encountered, we need to provide an interface for this kind of
  111. * customization. The object format is also an argument of the callback.
  112. *
  113. * The settings defined in this form are stored in database by the filter
  114. * module, and they will be available in the $filter argument.
  115. */
  116. function _filter_example_filter_foo_settings($form, $form_state, $filter, $format, $defaults) {
  117. $settings['filter_example_foo'] = array(
  118. '#type' => 'textfield',
  119. '#title' => t('Substitution string'),
  120. '#default_value' => isset($filter->settings['filter_example_foo']) ? $filter->settings['filter_example_foo'] : $defaults['filter_example_foo'],
  121. '#description' => t('The string to substitute for "foo" everywhere in the text.')
  122. );
  123. return $settings;
  124. }
  125. /**
  126. * Foo filter process callback.
  127. *
  128. * The actual filtering is performed here. The supplied text should be returned,
  129. * once any necessary substitutions have taken place. The example just replaces
  130. * foo with our custom defined string in the settings page.
  131. */
  132. function _filter_example_filter_foo_process($text, $filter, $format) {
  133. $replacement = isset($filter->settings['filter_example_foo']) ? $filter->settings['filter_example_foo'] : 'bar';
  134. return str_replace('foo', $replacement, $text);
  135. }
  136. /**
  137. * Filter tips callback for foo filter.
  138. *
  139. * The tips callback allows filters to provide help text to users during the
  140. * content editing process. Short tips are provided on the content editing
  141. * screen, while long tips are provided on a separate linked page. Short tips
  142. * are optional, but long tips are highly recommended.
  143. */
  144. function _filter_example_filter_foo_tips($filter, $format, $long = FALSE) {
  145. $replacement = isset($filter->settings['filter_example_foo']) ? $filter->settings['filter_example_foo'] : 'bar';
  146. if (!$long) {
  147. // This string will be shown in the content add/edit form
  148. return t('<em>foo</em> replaced with %replacement.', array('%replacement' => $replacement));
  149. }
  150. else {
  151. return t('Every instance of "foo" in the input text will be replaced with a configurable value. You can configure this value and put whatever you want there. The replacement value is "%replacement".', array('%replacement' => $replacement));
  152. }
  153. }
  154. /**
  155. * Time filter prepare callback.
  156. *
  157. * We'll use [filter-example-time] as a replacement for the time tag.
  158. * Note that in a more complicated filter a closing tag may also be
  159. * required. For more information, see "Temporary placeholders and
  160. * delimiters" at http://drupal.org/node/209715.
  161. */
  162. function _filter_example_filter_time_prepare($text, $filter) {
  163. return preg_replace('!<time ?/>!', '[filter-example-time]', $text);
  164. }
  165. /**
  166. * Time filter process callback.
  167. *
  168. * Now, in the "process" step, we'll search for our escaped time tags and
  169. * do the real filtering: replace the xml tag with the date.
  170. */
  171. function _filter_example_filter_time_process($text, $filter) {
  172. return str_replace('[filter-example-time]', '<em>' . format_date(time()) . '</em>', $text);
  173. }
  174. /**
  175. * Filter tips callback for time filter.
  176. *
  177. * The tips callback allows filters to provide help text to users during the
  178. * content editing process. Short tips are provided on the content editing
  179. * screen, while long tips are provided on a separate linked page. Short tips
  180. * are optional, but long tips are highly recommended.
  181. */
  182. function _filter_example_filter_time_tips($filter, $format, $long = FALSE) {
  183. return t('<em>&lt;time /&gt;</em> is replaced with the current time.');
  184. }
  185. /**
  186. * @} End of "defgroup filter_example".
  187. */