The current filtering system is a global based filtering system such as filtering urls, converting \n to <p>, <br/> which is really good, but in doing some complex things such as quickly rendering some information from the current node is not always possible since there is no direct link between the text being filtered and the entity (node, comment, transaction etc) which is comes from.

One example of this is when using the PHP filter to build some text on node, to get the current node you need use the url, or the menu system to get the node object, which is fine on the when your url is node/xxx but when you are viewing a list of nodes, this is not possible.

This patch adds context to the current filtering system so that when you are filtering the text you also have access to the entity or any other variables that are relevant. So when rendering a text field, the filters will also have access to the node (if this field is on a node) directly.

This also opens the possibility to use less security problematic filters such as the PHP filter. Since Twig will be in core (fingers crossed), instead of using the PHP filter, we can use Twig to filter variables, and even control blocks and loops. Since Twig is all sandboxed, the ability to give access to a filter with much more control without the security implications.

Related Patches

#1996922: Twig Filter
#2002522: Token Filter

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

gordon’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 0001-Change-Text-Filters-so-that-they-can-have-contextual.patch, failed testing.

gordon’s picture

Here is a new version which takes into account the recent filter changes.

gordon’s picture

Status: Needs work » Needs review
cweagans’s picture

If PHP filter were a feature that should be used, this would be a good addition. However, PHP filter is highly discouraged by people who know what they're doing, so I don't think we should be extending this functionality in any way. I'm of the opinion that PHP filter should be removed from core entirely, and I would much more readily support.

I cannot, with good faith, support adding more code to core that allows new users to shoot themselves in the foot.

gordon’s picture

The PHP filter is the only case in core where is makes sense in core to use this, this is more for filters like Twig or any filter where having contextual access to relevant information will make things 100s of times easier.

In core this will add very little functionality, but the possibilities for contrib filtering modules, or 1 off filters for implementations will make people lives so much easier.

This functionality that can't be added to contrib, but will enhance contrib modules hugely.

I agree 100% removing the PHP filter from core is something that should be done, and having something like Twig replace it will make removing the PHP filter more possible.

cweagans’s picture

If you want contextual information in text filters, use token replacement. That works just fine. As I stated in the other issue, code does not belong in the UI.

gordon’s picture

I agree about code, and in a perfect world this would be great.

This patch is not about PHP or Twig, and adding code to filtered text, but it is about giving filtered (any type of filter) access to related information.

eg. you want to add a field/token into the body of your text which is another field on the current entity.

Right now to do this, you need to bend over backwards to get the entity that this relates to. ie. use the path to get the node id, or grab the node from the menu system. which BTW doesn't work on a list of teasers.

With this patch it is in the contextual information so adding a filter which gets the field and replaces it is easy. you have the entity right there, and the filter becomes easy.

Infact with this patch we could write a new token filter which allows placing of tokens from the token module and directly into your text.

sdboyer’s picture

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

same basic argument as from #1996910-10: Add context to system filters - feature requests are 9.x.

and, to make such a low-level API change, we should probably be ensuring this fits more tightly with typed data, rather than the simple "whatever you wanna give me" data passthrough that this patch represents.

gordon’s picture

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

I need to add something to this patch which will allow the context to be passed to the tips, which then will allow the tips to display the help on what variables are available.

see #2002522: Token Filter for an implementation of the token filter.

Status: Needs review » Needs work

The last submitted patch, 0001-Change-Text-Filters-so-that-they-can-have-contextual.patch, failed testing.

cweagans’s picture

Version: 8.x-dev » 9.x-dev

feature requests are 9.x.

Nobody can stop you from working on this patch, but it is not eligible for 8.x anymore.

gordon’s picture

Status: Needs work » Needs review
FileSize
37.19 KB

I have updated the patch to also pass the text context to the filter tips as well. I have updated the php filter to make sure of this.

gordon’s picture

Updated Patch

gordon’s picture

Issue summary: View changes

Add related patches

catch’s picture

Version: 9.x-dev » 8.1.x-dev

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

marvil07’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

The patch no longer applies.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

himanshu-dixit’s picture

Assigned: gordon » himanshu-dixit

I would like to work on reroll .

jofitz’s picture

Assigned: himanshu-dixit » Unassigned
Status: Needs work » Needs review
FileSize
21.75 KB

I assume @himanshu-dixit has moved on / given up / forgotten about this.

Here's my attempt at the re-roll.

jofitz’s picture

Issue tags: -Needs reroll

Status: Needs review » Needs work

The last submitted patch, 21: 1996910-21.patch, failed testing.

boaloysius’s picture

Assigned: Unassigned » boaloysius
boaloysius’s picture

Assigned: boaloysius » Unassigned

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

gordon’s picture

gordon’s picture

Status: Needs work » Needs review
FileSize
22.94 KB

Basically this is just a reroll of the previous patch.

gordon’s picture

Clean up formatting errors.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

id.conky’s picture

- Rerolled for 9.2.5
- Coverage for new filter plugins
- Populate context from different places where ProcessedText used

id.conky’s picture

Fixed error fatal by adding entity context to all form widgets (not the best option, need to refactor in future).

marvil07’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

Last two patches on #37 and #38 are not using core/ directory as prefix, but if apply them manually they can be used.
This needs a re-roll.

ankithashetty’s picture

ankithashetty’s picture

ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
2.65 KB

There is some difference between the #37 and #38 patches, hence re-rolled both of them separately. Attaching an diff file here between #40 and #41.

Thank you!

darvanen’s picture

Status: Needs review » Needs work

Marking NW for the phpcs failures.

Noting that #38 says:

Fixed error fatal by adding entity context to all form widgets (not the best option, need to refactor in future).

What then, is the best approach? Let's narrow this down to one approach, one patch, before marking it for review.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

prudloff’s picture

In order to maintain backward compatibility, $context should be the last parameter in FilterInterface::process() and should be optional.