token_module_test.module in Token 8
Helper module for token tests.
File
tests/modules/token_module_test/token_module_test.moduleView source
<?php
/**
* @file
* Helper module for token tests.
*/
use Drupal\node\NodeInterface;
/**
* Implements hook_page_attachments().
*/
function token_module_test_page_attachments() {
if ($debug = \Drupal::state()
->get('token_page_tokens', [])) {
$debug += [
'tokens' => [],
'data' => [],
'options' => [],
];
foreach (array_keys($debug['tokens']) as $token) {
$debug['values'][$token] = \Drupal::token()
->replace($token, $debug['data'], $debug['options']);
}
\Drupal::state()
->set('token_page_tokens', $debug);
}
}
/**
* Implements hook_ENTITY_TYPE_presave for Node entities.
*/
function token_module_test_node_presave(NodeInterface $node) {
// Transform tokens in the body.
// @see \Drupal\token\Tests\TokenMenuTest::testMenuTokens()
if ($node
->hasField('body')) {
$node->body->value = \Drupal::token()
->replace($node->body->value, [
'node' => $node,
]);
}
}
Functions
Name | Description |
---|---|
token_module_test_node_presave | Implements hook_ENTITY_TYPE_presave for Node entities. |
token_module_test_page_attachments | Implements hook_page_attachments(). |