Skip to content

Token integration

Drupal core replaces tokens for fields on entities (for example in text, Path, or custom code using \Drupal::token()). The bare field token uses Name’s formatter so output matches configured display formats. The Token contrib module adds a browsable token tree and related UI; it is not required for replacement to work.

Name does not implement hook_token(); behavior comes from core field tokens plus the name field formatter.

Requirements

  • Core only: Name and the entity or field setup you are tokenizing (e.g. Node).
  • Token module (optional): Install Token if you want the token browser and related helpers. Name lists token:token under test_dependencies in its info file, not as a runtime dependency.

Example tokens

Patterns below follow entity_type, field machine name, optional delta, and property. Adjust field_name, bundle, and machine names to match your site. Examples mirror functional coverage in Drupal\Tests\name\Functional\NameNodeTokenReplaceTest (node and user name fields).

Token Typical output
[node:field_name] Full name via formatter (all components the field uses)
[node:field_name:title] Title component
[node:field_name:given] Given name
[node:field_name:middle] Middle name(s)
[node:field_name:family] Family name
[node:field_name:generational] Generational suffix
[node:field_name:credentials] Credentials

For author references, user field tokens work the same way, for example [node:author:field_realname] and [node:author:field_realname:family], alongside core user tokens such as [node:author:display-name].

Multiple values

Delta-specific tokens (e.g. a second value on a multi-value field) follow core field token rules; see Drupal core field token documentation.

Site builder notes

  • The bare field token ([node:YOUR_FIELD]) reflects formatter settings for that field instance.
  • Per-property tokens return the raw component strings.
  • Install the Token module when editors need to pick tokens from a UI instead of memorizing patterns.

See also