5 * Hooks related to the Token system.
8 use Drupal\user\Entity\User;
16 * Provide replacement values for placeholder tokens.
18 * This hook is invoked when someone calls
19 * \Drupal\Core\Utility\Token::replace(). That function first scans the text for
20 * [type:token] patterns, and splits the needed tokens into groups by type.
21 * Then hook_tokens() is invoked on each token-type group, allowing your module
22 * to respond by providing replacement text for any of the tokens in the group
23 * that your module knows how to process.
25 * A module implementing this hook should also implement hook_token_info() in
26 * order to list its available tokens on editing screens.
29 * The machine-readable name of the type (group) of token being replaced, such
30 * as 'node', 'user', or another type defined by a hook_token_info()
33 * An array of tokens to be replaced. The keys are the machine-readable token
34 * names, and the values are the raw [type:token] strings that appeared in the
37 * An associative array of data objects to be used when generating replacement
38 * values, as supplied in the $data parameter to
39 * \Drupal\Core\Utility\Token::replace().
40 * @param array $options
41 * An associative array of options for token replacement; see
42 * \Drupal\Core\Utility\Token::replace() for possible values.
43 * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata
44 * The bubbleable metadata. Prior to invoking this hook,
45 * \Drupal\Core\Utility\Token::generate() collects metadata for all of the
46 * data objects in $data. For any data sources not in $data, but that are
47 * used by the token replacement logic, such as global configuration (e.g.,
48 * 'system.site') and related objects (e.g., $node->getOwner()),
49 * implementations of this hook must add the corresponding metadata.
52 * $bubbleable_metadata->addCacheableDependency(\Drupal::config('system.site'));
53 * $bubbleable_metadata->addCacheableDependency($node->getOwner());
56 * Additionally, implementations of this hook, must forward
57 * $bubbleable_metadata to the chained tokens that they invoke.
60 * if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) {
61 * $replacements = $token_service->generate('date', $created_tokens, array('date' => $node->getCreatedTime()), $options, $bubbleable_metadata);
66 * An associative array of replacement values, keyed by the raw [type:token]
67 * strings from the original text. The returned values must be either plain
68 * text strings, or an object implementing MarkupInterface if they are
71 * @see hook_token_info()
72 * @see hook_tokens_alter()
74 function hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
75 $token_service = \Drupal::token();
77 $url_options = ['absolute' => TRUE];
78 if (isset($options['langcode'])) {
79 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
80 $langcode = $options['langcode'];
87 if ($type == 'node' && !empty($data['node'])) {
88 /** @var \Drupal\node\NodeInterface $node */
89 $node = $data['node'];
91 foreach ($tokens as $name => $original) {
93 // Simple key values on the node.
95 $replacements[$original] = $node->nid;
99 $replacements[$original] = $node->getTitle();
103 $replacements[$original] = $node->url('edit-form', $url_options);
106 // Default values for the chained tokens handled below.
108 $account = $node->getOwner() ? $node->getOwner() : User::load(0);
109 $replacements[$original] = $account->label();
110 $bubbleable_metadata->addCacheableDependency($account);
114 $replacements[$original] = format_date($node->getCreatedTime(), 'medium', '', NULL, $langcode);
119 if ($author_tokens = $token_service->findWithPrefix($tokens, 'author')) {
120 $replacements += $token_service->generate('user', $author_tokens, ['user' => $node->getOwner()], $options, $bubbleable_metadata);
123 if ($created_tokens = $token_service->findWithPrefix($tokens, 'created')) {
124 $replacements += $token_service->generate('date', $created_tokens, ['date' => $node->getCreatedTime()], $options, $bubbleable_metadata);
128 return $replacements;
132 * Alter replacement values for placeholder tokens.
134 * @param $replacements
135 * An associative array of replacements returned by hook_tokens().
137 * The context in which hook_tokens() was called. An associative array with
138 * the following keys, which have the same meaning as the corresponding
139 * parameters of hook_tokens():
144 * @param \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata
145 * The bubbleable metadata. In case you alter an existing token based upon
146 * a data source that isn't in $context['data'], you must add that
147 * dependency to $bubbleable_metadata.
151 function hook_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
152 $options = $context['options'];
154 if (isset($options['langcode'])) {
155 $url_options['language'] = \Drupal::languageManager()->getLanguage($options['langcode']);
156 $langcode = $options['langcode'];
162 if ($context['type'] == 'node' && !empty($context['data']['node'])) {
163 $node = $context['data']['node'];
165 // Alter the [node:title] token, and replace it with the rendered content
166 // of a field (field_title).
167 if (isset($context['tokens']['title'])) {
168 $title = $node->field_title->view('default');
169 $replacements[$context['tokens']['title']] = drupal_render($title);
175 * Provide information about available placeholder tokens and token types.
177 * Tokens are placeholders that can be put into text by using the syntax
178 * [type:token], where type is the machine-readable name of a token type, and
179 * token is the machine-readable name of a token within this group. This hook
180 * provides a list of types and tokens to be displayed on text editing screens,
181 * so that people editing text can see what their token options are.
183 * The actual token replacement is done by
184 * \Drupal\Core\Utility\Token::replace(), which invokes hook_tokens(). Your
185 * module will need to implement that hook in order to generate token
186 * replacements from the tokens defined here.
189 * An associative array of available tokens and token types. The outer array
190 * has two components:
191 * - types: An associative array of token types (groups). Each token type is
192 * an associative array with the following components:
193 * - name: The translated human-readable short name of the token type.
194 * - description (optional): A translated longer description of the token
196 * - needs-data: The type of data that must be provided to
197 * \Drupal\Core\Utility\Token::replace() in the $data argument (i.e., the
198 * key name in $data) in order for tokens of this type to be used in the
199 * $text being processed. For instance, if the token needs a node object,
200 * 'needs-data' should be 'node', and to use this token in
201 * \Drupal\Core\Utility\Token::replace(), the caller needs to supply a
202 * node object as $data['node']. Some token data can also be supplied
203 * indirectly; for instance, a node object in $data supplies a user object
204 * (the author of the node), allowing user tokens to be used when only
205 * a node data object is supplied.
206 * - tokens: An associative array of tokens. The outer array is keyed by the
207 * group name (the same key as in the types array). Within each group of
208 * tokens, each token item is keyed by the machine name of the token, and
209 * each token item has the following components:
210 * - name: The translated human-readable short name of the token.
211 * - description (optional): A translated longer description of the token.
212 * - type (optional): A 'needs-data' data type supplied by this token, which
213 * should match a 'needs-data' value from another token type. For example,
214 * the node author token provides a user object, which can then be used
215 * for token replacement data in \Drupal\Core\Utility\Token::replace()
216 * without having to supply a separate user object.
218 * @see hook_token_info_alter()
221 function hook_token_info() {
223 'name' => t('Nodes'),
224 'description' => t('Tokens related to individual nodes.'),
225 'needs-data' => 'node',
228 // Core tokens for nodes.
230 'name' => t("Node ID"),
231 'description' => t("The unique ID of the node."),
234 'name' => t("Title"),
236 $node['edit-url'] = [
237 'name' => t("Edit URL"),
238 'description' => t("The URL of the node's edit page."),
241 // Chained tokens for nodes.
243 'name' => t("Date created"),
247 'name' => t("Author"),
252 'types' => ['node' => $type],
253 'tokens' => ['node' => $node],
258 * Alter the metadata about available placeholder tokens and token types.
261 * The associative array of token definitions from hook_token_info().
263 * @see hook_token_info()
265 function hook_token_info_alter(&$data) {
266 // Modify description of node tokens for our site.
267 $data['tokens']['node']['nid'] = [
268 'name' => t("Node ID"),
269 'description' => t("The unique ID of the article."),
271 $data['tokens']['node']['title'] = [
272 'name' => t("Title"),
273 'description' => t("The title of the article."),
276 // Chained tokens for nodes.
277 $data['tokens']['node']['created'] = [
278 'name' => t("Date created"),
279 'description' => t("The date the article was posted."),
285 * @} End of "addtogroup hooks".