Problem/Motivation

Port D7 version of filter_example to the 4.0.x branch (Drupal 9.4+).

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#39 drupal.org_files_issues_2023-02-19_2277629-39.patch16.94 KBmrinalini9
#34 interdiff-33-34.txt4.79 KBjungle
#34 2277629-34.patch16.95 KBjungle
#33 2277629-33.patch16.67 KBjungle
#31 filter_example-2277629-31.patch17.03 KBjrockowitz
#25 interdiff.txt468 bytesjlbellido
#25 filter_Example_2277629-24.patch16.11 KBjlbellido
#23 interdiff.txt1.77 KBjlbellido
#23 filter_Example_2277629-23.patch16.1 KBjlbellido
#23 2277629_filter_example_HTML_chars.png14.85 KBjlbellido
#23 2277629_filter_example_manual_test.png42.14 KBjlbellido
#22 foo_time.jpg27.55 KBMile23
#20 filter_Example_2277629-20.patch15.88 KBnavneet0693
#17 filter_Example_2277629-17.patch15.64 KBnavneet0693
#17 interdiff-13-17.txt2.22 KBnavneet0693
#13 filter_Example_2277629-13.patch14.12 KBnavneet0693
#12 filter_Example_2277629-10.patch14.12 KBnavneet0693
#12 interdiff-11-12.txt23.96 KBnavneet0693
#11 filter_Example_2277629-10.patch13.92 KBVj
#9 filter_Example_2277629-9.patch13.37 KBVj
#7 filter_Example_2277629-7.patch6.26 KBVj
#4 filter_example-2277629-4.patch11.96 KBrosinegrean
#4 interdiff-2-4.txt2.33 KBrosinegrean
#2 filter_example-2277629-2.patch11.93 KBrosinegrean
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rosinegrean’s picture

Assigned: Unassigned » rosinegrean
rosinegrean’s picture

Status: Active » Needs review
FileSize
11.93 KB

This is a first version. I still have 2 fatal errors in my local, but wanted to be sure I'm on the right track.

Status: Needs review » Needs work

The last submitted patch, 2: filter_example-2277629-2.patch, failed testing.

rosinegrean’s picture

Status: Needs work » Needs review
FileSize
2.33 KB
11.96 KB

Status: Needs review » Needs work

The last submitted patch, 4: filter_example-2277629-4.patch, failed testing.

Mile23’s picture

Assigned: rosinegrean » Unassigned

Generally unassigning issues. Please re-assign yourself as desired.

Vj’s picture

Assigned: Unassigned » Vj
Status: Needs work » Needs review
FileSize
6.26 KB

Initial patch without test case. Will update new patch with tests soon.

Status: Needs review » Needs work

The last submitted patch, 7: filter_Example_2277629-7.patch, failed testing. View results

Vj’s picture

Status: Needs work » Needs review
FileSize
13.37 KB

Final patch with tests & documentation.

Please review and let me know if any changes required.

navneet0693’s picture

Status: Needs review » Needs work

Gave a quick review on the patch.

  1. +++ b/filter_example/filter_example.info.yml
    @@ -0,0 +1,7 @@
    +  - filter
    

    drupal:filter

  2. +++ b/filter_example/filter_example.info.yml
    @@ -0,0 +1,7 @@
    \ No newline at end of file
    
    +++ b/filter_example/filter_example.links.menu.yml
    @@ -0,0 +1,4 @@
    \ No newline at end of file
    
    +++ b/filter_example/filter_example.module
    @@ -0,0 +1,34 @@
    \ No newline at end of file
    
    +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,83 @@
    \ No newline at end of file
    

    News line at end :-)

  3. +++ b/filter_example/src/Controller/FilterExampleController.php
    @@ -0,0 +1,29 @@
    +      '#markup' => '<p>' . $this->t('This example provides two filters.') . '</p><p>' . $this->t('Foo Filter replaces "foo" with a configurable replacement.') . '</p><p>' . $this->t('Time Tag replaces the string
    

    you can use DescriptionTemplateTrait for long inline description.

  4. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,83 @@
    +      return $this->t('<em>foo</em> replaced with %replacement.', array('%replacement' => $replacement));
    

    You can use %foo instead of tags and replace it just like we did for replacement.

  5. +++ b/filter_example/tests/src/Functional/FilterExampleTest.php
    @@ -0,0 +1,104 @@
    +  public static $modules = ['node', 'filter', 'filter_test', 'filter_example'];
    

    Do we really need to enable filter_test and filter_example module for testing ?

Vj’s picture

@navneet0693

Thanks for the review. I have re-rolled the patch. Please let me know if any changes required.

navneet0693’s picture

Added changes in tests, removed depreciated function. Added few changes in module. Ouch please ignore wrong file name :(

navneet0693’s picture

The last submitted patch, 12: filter_Example_2277629-10.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 13: filter_Example_2277629-13.patch, failed testing. View results

navneet0693’s picture

I think I need to add 'filter_test' for enabling filtered html and full html filter formats.

@Vj thank you for pointing me out!

navneet0693’s picture

Status: Needs work » Needs review
FileSize
2.22 KB
15.64 KB

I have added a sub-module 'filter_example_test' in tests so as to import the configurations.

eojthebrave’s picture

Status: Needs review » Needs work

This is great. I learned a bunch about how to write a filter by reviewing this code. Thank you. Here's a couple of things I found in the process that I think we can improve before committing this.

  1. +++ b/filter_example/filter_example.module
    @@ -0,0 +1,35 @@
    + * 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.
    

    I find this paragraph to be kind of confusing. I believe what it is trying to say is that each filter can be configured differently for each of the text formats where it is enabled. This example filter provides configuration that will allow an administrator to determine what string should be replaced with the text "foo".

  2. +++ b/filter_example/filter_example.module
    @@ -0,0 +1,35 @@
    + * 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.
    + */
    

    I think it might be worth adding an @see to \Drupal\filter\Plugin\FilterInterface which has some pretty good high level documentation on the Filter system and how it works.

    As well as an @link for https://www.drupal.org/docs/8/api/filter-api

  3. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,84 @@
    + * Drupal has several text formats (they are not filters), and in our example
    + * the foo replacement can be configured for each one of them, so the module
    + * includes a settingsForm method, with options to configure those replacements.
    + * Also, a Tips method will help showing the current replacement
    + * for the content type being edited.
    

    This comment needs to be wrapped to 80 characters.

  4. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,84 @@
    + * @Filter(
    

    I think we should add an @see to \Drupal\filter\Annotation\Filter which documents the Annotation used here.

    And maybe also \Drupal\filter\Plugin\FilterInterface

  5. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,84 @@
    + *   id = "filter_foo",
    

    This is maybe nit-picky, but the standard here appears to be {MODULE_NAME}_{FILTER}. So this might be better as "filter_example_foo" so as to not possibly conflict with something in the filer module's namespace.

  6. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,84 @@
    +   * The settings defined in this form are stored in database by the filter
    +   * module, and they will be available in the $this->settings.
    

    "stored in the database" ... sentence is missing a "the".

  7. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,84 @@
    +   * 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.
    

    This comment should be wrapped to 80 characters.

  8. +++ b/filter_example/src/Plugin/Filter/FilterTime.php
    @@ -0,0 +1,56 @@
    +/**
    + * Provides a filter to replace "foo".
    + *
    + * When used in combination with the filter_align filter, this must run last.
    + *
    + * @Filter(
    + *   id = "filter_time",
    + *   title = @Translation("Time Tag (example)"),
    + *   description = @Translation("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."),
    + *   type = Drupal\filter\Plugin\FilterInterface::TYPE_MARKUP_LANGUAGE,
    + * )
    + */
    

    This should also get an @see for \Drupal\filter\Annotation\Filter and \Drupal\filter\Plugin\FilterInterface

    And the id in the annotation should probably be filter_example_time.

  9. +++ b/filter_example/src/Plugin/Filter/FilterTime.php
    @@ -0,0 +1,56 @@
    +  /**
    +   * Time filter prepare callback.
    +   *
    +   * We'll use [filter-example-time] as a replacement for the time tag.
    +   * Note that in a more complicated filter a closing tag may also be
    +   * required.
    +   */
    +  public function prepare($text, $langcode) {
    +    return preg_replace('!<time ?/>!', '[filter-example-time]', $text);
    +  }
    

    This is a cool, and also kind of edge-case technique. I think it's probably worth expanding this comment a little to explain what is going on here, and when this code is going to be called.

navneet0693’s picture

Assigned: Vj » navneet0693

Needs re-roll !

navneet0693’s picture

navneet0693’s picture

Assigned: navneet0693 » Unassigned

@eojthebrave thank you for review :-)

Mile23’s picture

Title: Port D7 Tests To D8 for filter_example » Port filter_example for D8
Status: Needs review » Needs work
Issue tags: -Novice
FileSize
27.55 KB
  1. +++ b/filter_example/src/Plugin/Filter/FilterFoo.php
    @@ -0,0 +1,87 @@
    + *   description = @Translation("Every instance of 'foo' in the input text will be replaced with a preconfigured replacement."),
    

    Substitute 'configurable' for 'preconfigured.'

  2. +++ b/filter_example/templates/description.html.twig
    @@ -0,0 +1,15 @@
    +<p>To use these filters, go to <a href="{{ filter_admin_overview }}">admin/config/content/formats</a> and configure an input format, or create a new one.</p>
    

    Needs better instructions. I suggest: "Edit whichever text format you're using to include these filters. Make sure Time filter happens before 'Limit HTML tags'." And whatever else is required... I couldn't get the time filter to work. See below.

  3. +++ b/filter_example/tests/src/Functional/FilterExampleTest.php
    @@ -0,0 +1,105 @@
    +class FilterExampleTest extends BrowserTestBase {
    

    The test looks pretty good, and passes locally. It adds a time tag and passes in the test, but I was unable to get the time filter to work in a live setting.

jlbellido’s picture

I'm attaching a new patch including the comments from #22.

Additionally I've checked the example in a live site and I could get the time filter running. The issue was that the WYSIWYG is encoding and therefore the replacement doesn't work. If you include the tag without directly, it works. It would explain why you couldn't reproduce it and the tests passed.

I'm attaching screenshots with my local example.

Status: Needs review » Needs work

The last submitted patch, 23: filter_Example_2277629-23.patch, failed testing. View results

jlbellido’s picture

Status: Needs work » Needs review
FileSize
16.11 KB
468 bytes

I'm attaching a new patch that hopefully will solve Tests errors.

jlbellido’s picture

Issue tags: +drupaldevdays

Adding tags.

Mile23’s picture

Status: Needs review » Needs work

I still can't get the time filter to work. Here's how d.o swallows the instructions in #23:

If you include the tag without directly, it works

And it still looks like the image in #22.

Steps I took, which follow the instructions in the module:

  • Edited text formats at admin/config/content/formats.
  • Clicked on 'Configure' for Basic HTML.
  • Enabled Time Tag and Foo Filter.
  • Rearranged the filters so Time Tag is first, before 'Limit allowed HTML tags.'
  • Saved.

I also tried adding time as an allowed HTML tag, and I also tried disabling 'limit allowed HTML.'

Using the replacement filter [filter-example-time] works. But if we just change the filter to use that, then we don't get to demonstrate prepare().

It only seems to work in the non-WYSIWYG format. So is there a way to include WYSIWYG?

Mile23’s picture

+++ b/filter_example/tests/src/Functional/FilterExampleTest.php
@@ -0,0 +1,105 @@
+ * @ingroup filter_example
+ * @group examples

Also: Needs both @ingroup filter_example and @group filter_example

Mile23’s picture

Issue summary: View changes
Mile23’s picture

jrockowitz’s picture

I wanted to use the fiter_example.module during some training and saw that it had not been ported. I have limited bandwidth but I wanted to nudge this patch forward.

Attached is the patch from #25 updated to work with Drupal 9.x and Examples 3.x. Because of the moving for filter_example.module to the modules directory, an interdiff is not very useful. Moving forward we should continue to include interdiffs.

Changes

  • Moved 'filter_example' to 'modules/filter_example'
  • Fixed examples_toolbar()
  • Change core_version_requirement: ^9 in examples.info. @see #3211322: Loose requirements of examples module for Drupal 9.1.x
  • Added core_version_requirement: ^9 to filter_example.info.yml and filter_example_test.info.yml
  • Add $defaultTheme to FilterExampleTest.php
  • Change format_date() to use DateFormatter in \Drupal\filter_example\Plugin\Filter\FilterTime::process

Testing

  • Confirmed automated tests are passing
  • Manually tested both filters

Todo

  • Run the filter_example.module thru Drupal's coding standards
  • Determine if there are any blockers for this being committed
jungle’s picture

Version: 8.x-1.x-dev » 4.0.x-dev
Status: Needs review » Needs work

Thanks for working on this, it's close. 4.0.x is the latest dev branch for ^9.4 || ^10

jungle’s picture

Status: Needs work » Needs review
FileSize
16.67 KB
+++ b/modules/filter_example/filter_example.info.yml
@@ -0,0 +1,9 @@
+core_version_requirement: ^9

Rerolled and updated core_version_requirement to ^9.4 || ^10

jungle’s picture

Fix the test

jungle’s picture

Title: Port filter_example for D8 » Add filter example

D8 is EOL. The title is not applicable.

jungle’s picture

Issue summary: View changes
liquidcms’s picture

Status: Needs review » Needs work

patch does not apply to either 4.0.2 or latest 4.0-dev

liquidcms’s picture

In this example, would the config setting for filter_example_foo be translatable?

mrinalini9’s picture

Rerolled patch #34, please review it.

Thanks!

Status: Needs review » Needs work