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

  1. #2002134: Move TypedData metadata introspection from data objects to definition objects will allow us to retrieve typed data property definitions without needing data instances.
  2. #2002254: Add support for typed data selection will help us get the typed data values using token selectors.
  3. #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.
  4. Introduce a new “typed_data” (bool) property to token definitions.
  5. Update System module's token hook implementations. It will perform access control and return the replacement value (which implements TypedDataInterface) its getValue() return value. Token::replace() must accept an AccountInterface object for runtime access control.

User interface changes

None.

API changes

None.

CommentFileSizeAuthor
#9 drupal_2164635_9.patch2.4 KBXano
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

That 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?

Xano’s picture

The 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.

fago’s picture

Still, we could add a global context and expose it as tokens? Howsoever, let's make sure we cover the other stuff first.

Xano’s picture

We can always try to expose global data as typed data, but that is beyond the scope of this issue. See #2008386: Global contexts.

Dave Reid’s picture

I'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.

Xano’s picture

I 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.

fago’s picture

Issue tags: +d8rules

We are going to need this for Rules.

Xano’s picture

Issue summary: View changes
Xano’s picture

Status: Active » Needs review
FileSize
2.4 KB

A bit of pseudo code to illustrate how we could do this. It will have to be extended with support for other data types.

Status: Needs review » Needs work

The last submitted patch, 9: drupal_2164635_9.patch, failed testing.

fago’s picture

The 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.

+++ b/core/modules/system/system.tokens.inc
@@ -89,6 +99,30 @@ function system_token_info() {
+  if (strpos($type, 'typed_data:') === 0 && isset($data[$type])) {

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?

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

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should 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.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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.

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

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should 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.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.