Update Token system documentation for new drupal.org docs structure

Created on 15 April 2025, about 2 months ago

Problem/Motivation

The token system needs some better documentation. Working with tokens has been a complicated and obscure matter as long as I've worked with Drupal, and I can't seem to find any dedicated section for it under the new drupal.org documentation structure โ†’ .

I'm reaching out to figure out how much demand there is for reworking the existing docs and writing up new docs to fit the new structure. Also, please point me in the right direction if anyone's already working on this or if I'm jumping the gun. All feedback is very much welcome.

Proposed resolution

Since the token system is mainly used by site builders and developers instead of content editors or end users, it probably makes the most sense to add a section to the Develop wiki โ†’ , at least as far as I can tell. As the Drupal project itself describes the "token system" as a component of Drupal on its own, I reckon it makes sense to add a guide for it under the Drupal APIs โ†’ section -- even if it's a bit smaller than most APIs.

A dedicated section for tokens for site builders could be added to the main Drupal wiki or the Drupal User Guide, with examples on how to use it. This in turn could link back to the Token API section.

The Token API section might contain information about the following:

  • rationale for why the token system exists
  • how to replace tokens via code
  • how it works, in terms of scanning for tokens, linking replacements through hooks, and replacing into strings
  • how the hooks are structured, in both functional and OO style
  • what tokens are available by default
  • how tokens for entities work
  • ecosystem modules that work with the token system
  • an upgrade guide for older Drupal sites, similar to the Tokens for D6 -> D7 guide โ†’

Remaining tasks

  • Aggregate existing Token system docs for current and previous Drupal versions
  • Determine a structure for new docs that will be navigable and useful
  • Work out what docs to write
  • Determine best location for new documentation, obtain necessary stakeholder approvals
  • Write docs, get out for others to review
๐Ÿ“Œ Task
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component

token system

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia geoffreyr Sydney, AU / Gadigal country

Live updates comments and jobs are added and updated live.
  • Needs documentation

    A documentation change is requested elsewhere. For Drupal core (and possibly other projects), once the change has been committed, this status should be recorded in a change record node.

Sign in to follow issues

Comments & Activities

  • Issue created by @geoffreyr
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia geoffreyr Sydney, AU / Gadigal country

    I had a bit of free time and ended up starting work on the Token API guide. Here's what I have so far.

    The bits that I'm hoping will be most useful are the lists of available tokens and the hook implementation guides. It's pretty hard to generate lists of tokens in pretty-printed documented form so hopefully the former shouldn't change too much.
    I thought that we couldn't really explain the Token system without having some info about Token module, since it provides entity field tokens which are pretty critical for extending the functionality of Token API itself.

    I spent some time going over the Token API codebase and reviewing the ins and outs of each section, so hopefully it's reasonably accurate. I'd say there are more passes to do before it fully fits the "voice" of the Drupal community, but hopefully it's useful.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia geoffreyr Sydney, AU / Gadigal country

    Thinking about how https://www.drupal.org/node/390482 โ†’ provides a comprehensive list of D7 tokens with a lot of examples of chained tokens, but trying to replicate this would get very long and would be tedious to write. I wonder if there's some way to write a JS tool that does the same thing as Token module's Token Tree that lets people explore the list of default/Token module tokens and generate ones with the appropriate nesting... probably not something that would be hostable on drupal.org though.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia geoffreyr Sydney, AU / Gadigal country

    Noting also that the old Token module handbook at https://www.drupal.org/documentation/modules/token โ†’ has some extra functionality that our guide doesn't yet cover such as using token_element_validate in #element_validate. I think I'd probably want buy-in from the module maintainers themselves before I consider adding that -- don't want to duplicate their entire handbook without their say so beyond what's necessary to say what about Token module makes Token API really click.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Probably the better project

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States bnjmnm Ann Arbor, MI

    Everything else on the APIs page โ†’ is a core api - it works without any additional modules. I'd prefer a module API not be on a core documentation page, but if there are strong feelings to the contrary it should ad least be incredibly clear, probably on all pages, that installing the contrib Token module is necessary for any of this to work.

  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone

    The the APIs page โ†’ states that "Each version of Drupal introduces new APIs and keeps only some of the previous major versionsโ€™ APIs." For me, that is clear that the guide is for Drupal core API only. The benefits are that is that reader know the scope of the material and the maintenance is simplified. All contributed modules are documented at https://www.drupal.org/docs/extending-drupal/contributed-modules โ†’ . I do not think exceptions should be made.

    What needs to be reviewed here? What are the links?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States bnjmnm Ann Arbor, MI

    Thanks @quietone (and apologies to @geoffreyr ) I may have spoke too soon in #7 . I'm glad you responded as it motivated me to double check my findings and it turns out my IDE was delivering incomplete info.

    There is in fact a Core API and it's fine to have this as part of the drupal-apis โ†’ guide.

    Something to consider adding to the intro page โ†’ is an explanation of what this API does vs the Token module. More often than not, I wouldn't want this kind of pre-emptive clarification in a core API guide, but in this case it's an incredibly popular module that makes use of this identically-named API so it might be beneficial.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia geoffreyr Sydney, AU / Gadigal country

    @bnjmnm All good, no worries. I agree that I could do more to explain the difference between Token API and Token module, and why the latter is really useful for extending the capabilities of the former, while at the same time not being a requirement for Drupal core. I'll see if I can do this and get back to you and @quietone when I've made some progress.

Production build 0.71.5 2024