W3cubDocs

/Drupal 8

class Token

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:

// Load a node and a user, then replace tokens in the text.
$text = 'On [date:short], [user:name] read [node:title].';
$node = Node::load(1);
$user = User::load(1);

// [date:...] tokens use the current date automatically.
$data = array('node' => $node, 'user' => $user);
return Token::replace($text, $data);

Some tokens may be chained in the form of [$type:$pointer:$name], where $type is a normal token type, $pointer is a reference to another token type, and $name is the name of a given placeholder. For example, [node:author:mail]. In that example, 'author' is a pointer to the 'user' account that created the node, and 'mail' is a placeholder available for any 'user'.

Hierarchy

  • class \Drupal\Core\Utility\Token

See also

Token::replace()

hook_tokens()

hook_token_info()

File

core/lib/Drupal/Core/Utility/Token.php, line 60

Namespace

Drupal\Core\Utility

Members

Name Modifiers Type Description
Token::$cache protected property The token cache.
Token::$cacheTagsInvalidator protected property The cache tags invalidator.
Token::$languageManager protected property The language manager.
Token::$moduleHandler protected property The module handler service.
Token::$renderer protected property The renderer.
Token::$tokenInfo protected property Token definitions.
Token::findWithPrefix public function Returns a list of tokens that begin with a specific prefix.
Token::generate public function Generates replacement values for a list of tokens.
Token::getInfo public function Returns metadata describing supported tokens.
Token::replace public function Replaces all tokens in a given string with appropriate values.
Token::resetInfo public function Resets metadata describing supported tokens.
Token::scan public function Builds a list of all token-like patterns that appear in the text.
Token::setInfo public function Sets metadata describing supported tokens.
Token::TOKEN_INFO_CACHE_TAG constant The tag to cache token info with.
Token::__construct public function Constructs a new class instance.

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Utility!Token.php/class/Token/8.1.x