Token
class Token (View source)
Drupal placeholder/token replacement system.
API functions for replacing placeholders in text with meaningful values.
For example: When configuring automated emails, an administrator enters standard text for the email. Variables like the title of a node and the date the email was sent can be entered as placeholders like [node:title] and [date:short]. When a Drupal module prepares to send the email, it can call the Token::replace() function, passing in the text. The token system will scan the text for placeholder tokens, give other modules an opportunity to replace them with meaningful text, then return the final product to the original module.
Tokens follow the form: [$type:$name], where $type is a general class of tokens like 'node', 'user', or 'comment' and $name is the name of a given placeholder. For example, [node:title] or [node:created:since].
In addition to raw text containing placeholders, modules may pass in an array of objects to be used when performing the replacement. The objects should be keyed by the token type they correspond to. For example:
Constants
| TOKEN_INFO_CACHE_TAG |
The tag to cache token info with. |
Properties
| protected CacheBackendInterface | $cache | The token cache. |
|
| protected LanguageManagerInterface | $languageManager | The language manager. |
|
| protected array[]|null | $tokenInfo | Token definitions. |
|
| protected ModuleHandlerInterface | $moduleHandler | The module handler service. |
|
| protected CacheTagsInvalidatorInterface | $cacheTagsInvalidator | The cache tags invalidator. |
|
| protected RendererInterface | $renderer | The renderer. |
Methods
Constructs a new class instance.
Replaces all tokens in a given string with appropriate values.
Builds a list of all token-like patterns that appear in the text.
Generates replacement values for a list of tokens.
Returns a list of tokens that begin with a specific prefix.
Returns metadata describing supported tokens.
Sets metadata describing supported tokens.
Resets metadata describing supported tokens.
Details
__construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, CacheTagsInvalidatorInterface $cache_tags_invalidator, RendererInterface $renderer)
Constructs a new class instance.
string
replace(string $text, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata = NULL)
Replaces all tokens in a given string with appropriate values.
array
scan(string $text)
Builds a list of all token-like patterns that appear in the text.
array
generate(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata)
Generates replacement values for a list of tokens.
array
findWithPrefix(array $tokens, string $prefix, string $delimiter = ':')
Returns a list of tokens that begin with a specific prefix.
Used to extract a group of 'chained' tokens (such as [node:author:name]) from the full list of tokens found in text. For example:
array
getInfo()
Returns metadata describing supported tokens.
The metadata array contains token type, name, and description data as well as an optional pointer indicating that the token chains to another set of tokens.
setInfo(array $tokens)
Sets metadata describing supported tokens.
resetInfo()
Resets metadata describing supported tokens.