Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TokenProcessor - Allow basic filter/modifier expressions #21428

Merged
merged 3 commits into from
Sep 12, 2021

Conversation

totten
Copy link
Member

@totten totten commented Sep 10, 2021

Overview

This demonstrates how to extend TokenProcessor to support rendering token-expressions with filters/modifiers.

ping @eileenmcnaughton @mattwire

Before

Hello {foo.bar}

After

Hello {foo.bar|upper} and Hello {foo.bar|lower}

Technical Details

This only supports tokens that supply values through civi.token.eval (ie $row->token(...key-value...)). At the time of this commit, most {contact.*} tokens still use TokenCompatSubscriber::onRender() to hack-in their values and won't work until they switch over.

The regex which recognizes the filter is pretty tight (eg accepting |upper or |lower but not |myfunc:"yadda yadda"). This can be relaxed somewhat by a subsequent change, but I'd say such a change has a burden to demonstrate safety/interoparbility when running in Token-Smarty format. (e.g. demonstrate that matching of open/close symbols works correctly).

@civibot
Copy link

civibot bot commented Sep 10, 2021

(Standard links)

@civibot civibot bot added the master label Sep 10, 2021
@eileenmcnaughton
Copy link
Contributor

Cool - I have a feeling that |upper would already be pretty useful (I'm sure I've written tokens in the past to do just that....)

@eileenmcnaughton
Copy link
Contributor

test fails relate

Before
------

`Hello {foo.bar}`

After
-----

`Hello {foo.bar|upper}` and `Hello {foo.bar|lower}`

Technical Details
-----------------

This only supports tokens that supply values through `civi.token.eval`.  At the time of
this commit, most `{contact.*}` tokens still use `TokenCompatSubscriber::onRender()` to
hack-in their values and won't work until they switch over.

The regex which recognizes the filter is pretty tight (`\w+`).  This can be relaxed
somewhat by a subsequent change, but I'd say such a change has a burden to demonstrate
safety/interoparbility when running in Token-Smarty format.  (e.g.  demonstrate that
matching of open/close symbols works correctly).
@totten
Copy link
Member Author

totten commented Sep 10, 2021

test fails relate

Ah, it wasn't working on pseudo-tokens with a : in the name. Updated the regex. ByTypeTest is now passing locally... We'll see what Jenkins has to say...

@eileenmcnaughton
Copy link
Contributor

@totten I'm gonna give this merge-ready. It seems useful in itself & it works but obviously the real value is when we extend for dates. I've put a note in the dev-digest

@eileenmcnaughton eileenmcnaughton added the merge ready PR will be merged after a few days if there are no objections label Sep 11, 2021
@colemanw colemanw merged commit d0d0202 into civicrm:master Sep 12, 2021
@colemanw
Copy link
Member

Looks good to me too.

@totten totten deleted the master-tp-filter branch September 13, 2021 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-test master merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants