diff --git a/examples.module b/examples.module index 1333b6e..5d5fbd8 100644 --- a/examples.module +++ b/examples.module @@ -47,6 +47,7 @@ function examples_toolbar() { 'field_example' => 'field_example.description', 'field_permission_example' => 'field_permission_example.description', 'file_example' => 'file_example.fileapi', + 'filter_example' => 'filter_example.page', 'hooks_example' => 'hooks_example.description', 'js_example' => 'js_example.info', 'node_type_example' => 'config_node_type_example.description', diff --git a/filter_example/filter_example.info.yml b/filter_example/filter_example.info.yml index d2316ef..77b4f48 100644 --- a/filter_example/filter_example.info.yml +++ b/filter_example/filter_example.info.yml @@ -1,8 +1,9 @@ -name: Filter example +name: 'Filter example' type: module description: 'An example module showing how to define a custom filter.' -package: Example modules +package: 'Example modules' core: 8.x dependencies: - drupal:node + - drupal:filter - examples:examples diff --git a/filter_example/filter_example.links.menu.yml b/filter_example/filter_example.links.menu.yml index ce1d260..217e725 100644 --- a/filter_example/filter_example.links.menu.yml +++ b/filter_example/filter_example.links.menu.yml @@ -1,4 +1,4 @@ filter_example.page: - title: Filter Example - route_name: filter_example.page - expanded: TRUE + title: 'Filter Example' + route_name: 'filter_example.page' + expanded: 'TRUE' diff --git a/filter_example/filter_example.module b/filter_example/filter_example.module index d246049..7141478 100644 --- a/filter_example/filter_example.module +++ b/filter_example/filter_example.module @@ -1,34 +1,35 @@ -, and replace it by the current time. - * - * Foo filter - * - * Drupal has several content formats (they are not filters), and in our example - * the foo replacement can be configured for each one of them, allowing an html - * or php replacement, so the module includes a settings callback, with options - * to configure that replacements. Also, a Tips callback will help showing the - * current replacement for the content type being 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 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. - */ +, and replace it by the current time. + * + * Foo filter + * + * Drupal has several content formats (they are not filters), and in our example + * the foo replacement can be configured for each one of them, allowing an html + * or php replacement, so the module includes a settings callback, with options + * to configure that replacements. Also, a Tips callback will help showing the + * current replacement for the content type being 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 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. + */ diff --git a/filter_example/src/Controller/FilterExampleController.php b/filter_example/src/Controller/FilterExampleController.php index 92f33b5..37442f2 100644 --- a/filter_example/src/Controller/FilterExampleController.php +++ b/filter_example/src/Controller/FilterExampleController.php @@ -1,24 +1,22 @@ -settings. - */ - public function settingsForm(array $form, FormStateInterface $form_state) { - $form['filter_example_foo'] = [ - '#type' => 'textfield', - '#title' => $this->t('Substitution string'), - '#default_value' => $this->settings['filter_example_foo'], - '#description' => $this->t('The string to substitute for "foo" everywhere in the text.'), - ]; - return $form; - } - - /** - * Foo filter process callback. - * - * The actual filtering is performed here. The supplied text should be returned, - * once any necessary substitutions have taken place. The example just replaces - * foo with our custom defined string in the settings page. - */ - public function process($text, $langcode) { - $replace = $this->settings['filter_example_foo']; - $new_text = str_replace('foo', $replace, $text); - return new FilterProcessResult($new_text); - } - - /** - * Filter tips callback for foo filter. - * - * The tips callback allows filters to provide help text to users during the - * content editing process. Short tips are provided on the content editing - * screen, while long tips are provided on a separate linked page. Short tips - * are optional, but long tips are highly recommended. - */ - public function tips($long = FALSE) { - $replacement = $this->settings['filter_example_foo']; - if (!$long) { - // This string will be shown in the content add/edit form. - return $this->t('%foo replaced with %replacement.', array('%foo' => 'foo', '%replacement' => $replacement)); - } - else { - return $this->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)); - } - } - -} +settings. + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $form['filter_example_foo'] = [ + '#type' => 'textfield', + '#title' => $this->t('Substitution string'), + '#default_value' => $this->settings['filter_example_foo'], + '#description' => $this->t('The string to substitute for "foo" everywhere in the text.'), + ]; + return $form; + } + + /** + * Foo filter process callback. + * + * The actual filtering is performed here. The supplied text should be + * returned once any necessary substitutions have taken place. + * The example just replaces foo with our custom defined string in + * the settings page. + */ + public function process($text, $langcode) { + $replace = $this->settings['filter_example_foo']; + $new_text = str_replace('foo', $replace, $text); + return new FilterProcessResult($new_text); + } + + /** + * Filter tips callback for foo filter. + * + * The tips callback allows filters to provide help text to users during the + * content editing process. Short tips are provided on the content editing + * screen, while long tips are provided on a separate linked page. Short tips + * are optional, but long tips are highly recommended. + */ + public function tips($long = FALSE) { + $replacement = $this->settings['filter_example_foo']; + if (!$long) { + // This string will be shown in the content add/edit form. + return $this->t('%foo replaced with %replacement.', ['%foo' => 'foo', '%replacement' => $replacement]); + } + else { + return $this->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".', ['%replacement' => $replacement]); + } + } + +} diff --git a/filter_example/src/Plugin/Filter/FilterTime.php b/filter_example/src/Plugin/Filter/FilterTime.php index 6c48694..edfaa75 100644 --- a/filter_example/src/Plugin/Filter/FilterTime.php +++ b/filter_example/src/Plugin/Filter/FilterTime.php @@ -1,56 +1,56 @@ -!', '[filter-example-time]', $text); - } - - /** - * Time filter process callback. - * - * Now, in the "process" step, we'll search for our escaped time tags and - * do the real filtering: replace the xml tag with the date. - */ - public function process($text, $langcode) { - $new_text = str_replace('[filter-example-time]', '' . format_date(time()) . '', $text); - return new FilterProcessResult($new_text); - } - - /** - * Filter tips callback for time filter. - * - * The tips callback allows filters to provide help text to users during the - * content editing process. Short tips are provided on the content editing - * screen, while long tips are provided on a separate linked page. Short tips - * are optional, but long tips are highly recommended. - */ - public function tips($long = FALSE) { - return $this->t('<time /> is replaced with the current time.'); - } - -} \ No newline at end of file +!', '[filter-example-time]', $text); + } + + /** + * Time filter process callback. + * + * Now, in the "process" step, we'll search for our escaped time tags and + * do the real filtering: replace the xml tag with the date. + */ + public function process($text, $langcode) { + $new_text = str_replace('[filter-example-time]', '' . format_date(time()) . '', $text); + return new FilterProcessResult($new_text); + } + + /** + * Filter tips callback for time filter. + * + * The tips callback allows filters to provide help text to users during the + * content editing process. Short tips are provided on the content editing + * screen, while long tips are provided on a separate linked page. Short tips + * are optional, but long tips are highly recommended. + */ + public function tips($long = FALSE) { + return $this->t('<time /> is replaced with the current time.'); + } + +} diff --git a/filter_example/tests/src/Functional/FilterExampleTest.php b/filter_example/tests/src/Functional/FilterExampleTest.php index 5735441..dfd8e34 100644 --- a/filter_example/tests/src/Functional/FilterExampleTest.php +++ b/filter_example/tests/src/Functional/FilterExampleTest.php @@ -1,104 +1,105 @@ -filteredHtml = FilterFormat::load('filtered_html'); - $this->fullHtml = FilterFormat::load('full_html'); - // Create users. - $this->adminUser = $this->drupalCreateUser([ - 'administer filters', - $this->filteredHtml->getPermissionName(), - $this->fullHtml->getPermissionName(), - 'bypass node access' - ]); - } - - /** - * Functional test of the foo filter. - * - * Login user, create an example node, and test filter functionality through - * the admin user interfaces. - */ - public function testFilterExampleBasic() { - $this->drupalLogin($this->adminUser); - - $edit = array( - 'filters[filter_time][status]' => TRUE, - 'filters[filter_foo][status]' => TRUE, - ); - $this->drupalPostForm('admin/config/content/formats/manage/' . $this->filteredHtml->id(), $edit, t('Save configuration')); - $this->assertRaw(t('The text format %format has been updated.', ['%format' => $this->filteredHtml->label()])); - - $content_type = $this->drupalCreateContentType(); - - $edit = []; - $edit['title[0][value]'] = $this->randomMachineName(); - $edit['body[0][value]'] = 'What foo is it? it is