token.inc

token.inc

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'.

See also

token_replace()

hook_tokens()

hook_token_info()

File

includes/token.inc

Functions

Name Description
token_find_with_prefix Returns a list of tokens that begin with a specific prefix.
token_generate Generates replacement values for a list of tokens.
token_info Returns metadata describing supported tokens.
token_replace Replaces all tokens in a given string with appropriate values.
token_scan Builds a list of all token-like patterns that appear in the text.

© 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/includes!token.inc/7.x

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部