Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Updated: Comment #N
Problem/Motivation
In Drupal 7 entities and their properties could be exposed to the token API automatically through entity_token, a sub module of the contributed Entity module. Most of the module has been merged with Drupal core, but there is no automatic exposure to token API yet.
Proposed resolution
Make the System module expose typed data in hook_token_info()
, and perform replacements in system_tokens()
.
Remaining tasks
- #2002134: Move TypedData metadata introspection from data objects to definition objects will allow us to retrieve typed data property definitions without needing data instances.
- #2002254: Add support for typed data selection will help us get the typed data values using token selectors.
- #2163027: Type token returns in a typed data recognizable format is a preparatory issue that makes sure existing token definitions are compatible with typed data IDs.
- Introduce a new “typed_data” (bool) property to token definitions.
- Update System module's token hook implementations. It will perform access control and return the replacement value (which implements
TypedDataInterface
) itsgetValue()
return value.Token::replace()
must accept anAccountInterface
object for runtime access control.
User interface changes
None.
API changes
None.
Comment | File | Size | Author |
---|---|---|---|
#9 | drupal_2164635_9.patch | 2.4 KB | Xano |
Comments
Comment #1
fagoThat seems like a good plan to make tokens pick up what's there. We miss some way to deal with global contexts though which should be exposed as tokens as well - I thought we've an issue for that somewhere already though?
Comment #2
XanoThe other issue was to make token API totally dependent on typed data. As that breaks backwards compatibility, which is no longer allowed, this issue simply exposed typed data to the existing token API. This means global contexts remain untouched.
Comment #3
fagoStill, we could add a global context and expose it as tokens? Howsoever, let's make sure we cover the other stuff first.
Comment #4
XanoWe can always try to expose global data as typed data, but that is beyond the scope of this issue. See #2008386: Global contexts.
Comment #5
Dave ReidI'm still very wary of implementing any kind of solution for fields or typed data until we can also concurrently solve the issue of exposing a UI to let the user know which tokens are available. One without the other will continue to plague contrib and site builders.
Comment #6
XanoI understand your concerns, but those are two separate issues that will need to be tackled separately. For adding the UI, see #2167143: Add a token browser.
Comment #7
fagoWe are going to need this for Rules.
Comment #8
XanoComment #9
XanoA bit of pseudo code to illustrate how we could do this. It will have to be extended with support for other data types.
Comment #11
fagoThe general approach looks good. We probably can look at some of entity token module's code as it is implementing exactly that approach in d7 as well. It has to do quite some making back and forth between token types and entity property info types though, what makes the code quite more complex.
While this ensures there are no type clashes it sucks and adds burden to developers and site-builders if token types do not match regular data types from typed data which we are using everywhere else?