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
Comment | File | Size | Author |
---|---|---|---|
#42 | reroll_diff_1996910_40-41.txt | 2.65 KB | ankithashetty |
#41 | reroll_diff_1996910_38-41.txt | 23.19 KB | ankithashetty |
#41 | 1996910-41.patch | 25.85 KB | ankithashetty |
#40 | reroll_diff_1996910_37-40.txt | 24.14 KB | ankithashetty |
#40 | 1996910-40.patch | 26.71 KB | ankithashetty |
Comments
Comment #1
gordon CreditAttribution: gordon commentedComment #3
gordon CreditAttribution: gordon commentedHere is a new version which takes into account the recent filter changes.
Comment #4
gordon CreditAttribution: gordon commentedComment #5
cweagansIf 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.
Comment #6
gordon CreditAttribution: gordon commentedThe 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.
Comment #7
cweagansIf 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.
Comment #8
gordon CreditAttribution: gordon commentedI 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.
Comment #9
sdboyer CreditAttribution: sdboyer commentedsame 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.
Comment #10
gordon CreditAttribution: gordon commentedI 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.
Comment #12
cweagansNobody can stop you from working on this patch, but it is not eligible for 8.x anymore.
Comment #13
gordon CreditAttribution: gordon commentedI 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.
Comment #14
gordon CreditAttribution: gordon commentedUpdated Patch
Comment #14.0
gordon CreditAttribution: gordon commentedAdd related patches
Comment #15
catchComment #18
marvil07 CreditAttribution: marvil07 as a volunteer commentedThe patch no longer applies.
Comment #20
himanshu-dixit CreditAttribution: himanshu-dixit as a volunteer commentedI would like to work on reroll .
Comment #21
jofitz CreditAttribution: jofitz at ComputerMinds commentedI assume @himanshu-dixit has moved on / given up / forgotten about this.
Here's my attempt at the re-roll.
Comment #22
jofitz CreditAttribution: jofitz at ComputerMinds commentedComment #24
boaloysius CreditAttribution: boaloysius as a volunteer commentedComment #25
boaloysius CreditAttribution: boaloysius as a volunteer commentedComment #32
gordon CreditAttribution: gordon at Heydon Consulting commentedComment #33
gordon CreditAttribution: gordon at Heydon Consulting commentedBasically this is just a reroll of the previous patch.
Comment #34
gordon CreditAttribution: gordon at Heydon Consulting commentedClean up formatting errors.
Comment #37
id.conky CreditAttribution: id.conky as a volunteer and at Drupal Ukraine Community commented- Rerolled for 9.2.5
- Coverage for new filter plugins
- Populate context from different places where ProcessedText used
Comment #38
id.conky CreditAttribution: id.conky as a volunteer and at Drupal Ukraine Community commentedFixed error fatal by adding entity context to all form widgets (not the best option, need to refactor in future).
Comment #39
marvil07 CreditAttribution: marvil07 as a volunteer and at Adapt commentedLast 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.
Comment #40
ankithashettyRerolled patch in #37.
Comment #41
ankithashettyRerolled patch in #38.
Comment #42
ankithashettyThere 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!
Comment #43
darvanenMarking NW for the phpcs failures.
Noting that #38 says:
What then, is the best approach? Let's narrow this down to one approach, one patch, before marking it for review.
Comment #48
prudloff CreditAttribution: prudloff at Insite commentedIn order to maintain backward compatibility,
$context
should be the last parameter inFilterInterface::process()
and should be optional.