Once we've the typed data API in place for entities we have everything in place to navigate through entities similar to using tokens, e.g. you can do $comment->node->entity->title->value; I think we should leverage this and build the token system on top of it.

My initial thoughts:
- The token API should be built upon Typed Data, i.e. not depend on entity interface but on typed data interface
- Use the typed data API as source for support data types and available properties. For adding a new token you can add a new computed property.
- Care about ways of formatting data only based upon data types. It should be possible to register a new token format for e.g. integers, dates, nodes, or date fields.
- Use the Typed Data API to get the value then format the value.

A possible problem with that approach I see in providing tokens. While you can typed-data enable any kind of data you cannot add new computed properties to any kind of data. You can only do so if the implementing class allows you to, e.g. as entities do. So it wouldn't be a problem for entities but should we also support adding new tokens to e.g. a date field's item? Or to a module's custom non-entity data structure?

I'm not sure this would be an acceptable restriction?

Comments

xano’s picture

#2002254: Typed Data tokens: allow addressing, labeling and evaluating a specific value in the (content entity) Typed Data hierarchy — empowering ECA and Canvas adds a solution to select data from any typed data based on selectors. This can be leveraged by token API by simply using the tokens as data selectors. This will remove the need for 90% of the existing hook_tokens() implementations as drilling down the data will be done by core, similarly to how Rules does it now. A few other things that will need to be added before we can completely replace hook_token_info() and hook_tokens():
- Add global context plugins: tokens for the site, the current date and time, and the currently logged-in user are global. We need plugins to expose and fetch these typed data types, and in order to prevent collisions, we will put them in the "global" token namespace (site:name becomes global:site:name, for instance).
- In order for token browsers to work, we may need to find a way to expose non-global typed data as well.
- We need to find a solution for accessing extra information of non-complex data, such as timestamps, which can be fetched in different human-readable formats, or from which things like the year, day, and month can be extracted. A working solution would be to convert these data to complex types and use computed properties, but this has some drawbacks @fago and @berdir can probably explain better than I can.

dave reid’s picture

I'm just not really in favor of this change until we can get a working token browser in core because ensuring that this data can be exposed in an a friendly way in the UI is really, really important to me. TypedData concerns me since it's new and untested (it hasn't been used in contrib for a core cycle yet). I'm tempted to just mark this as postponed.

Some specifics:

Add global context plugins: tokens for the site, the current date and time, and the currently logged-in user are global. We need plugins to expose and fetch these typed data types, and in order to prevent collisions, we will put them in the "global" token namespace (site:name becomes global:site:name, for instance).

We need to consider this will break existing tokens and we need to provide an upgrade path for those tokens. I'm guessing that we'd have more tokens that have TypedData replacements but don't use the same 'name' as the D7 token equivalent.

xano’s picture

I'm just not really in favor of this change until we can get a working token browser in core because ensuring that this data can be exposed in an a friendly way in the UI is really, really important to me.

I agree on the importance, but not having a token browser (or a typed data browser) in core is not a regression.

We need to consider this will break existing tokens and we need to provide an upgrade path for those tokens.

This will definitely break existing tokens. This was done for a good reason, but we can probably just as easily not use the global namespace. An upgrade path seems impossible to me, simply because there is no knowing where tokens may have been used.

amateescu’s picture

A simple upgrade path could be just a mapping of old/new tokens.

xano’s picture

xano’s picture

Issue summary: View changes

Updated issue summary.

xano’s picture

Version: 8.x-dev » 9.x-dev
Issue summary: View changes

For a more realistic solution for Drupal 8, see #2164635: Automatically expose typed data to token API.

catch’s picture

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

This doesn't need to be against 9.x, it could likely be done with a bc layer, might be won't fix though?

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.

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.

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.

darvanen’s picture

Closed #2924819: Make Tokens a special case of Typed Data as a duplicate of this issue.

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.