function book_tokens in Token 8
Same name and namespace in other branches
- 7 token.tokens.inc \book_tokens()
Implements hook_tokens() on behalf of book.module.
File
- ./
token.tokens.inc, line 1222 - Token callbacks for the token module.
Code
function book_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
// Node tokens.
if ($type == 'node' && !empty($data['node'])) {
$book = $data['node']->book;
if (!empty($book['bid'])) {
if ($book_tokens = \Drupal::token()
->findWithPrefix($tokens, 'book')) {
$child_node = Node::load($book['nid']);
$replacements += \Drupal::token()
->generate('book', $book_tokens, [
'book' => $child_node,
], $options, $bubbleable_metadata);
}
}
}
elseif ($type == 'book' && !empty($data['book'])) {
$book = $data['book']->book;
if (!empty($book['bid'])) {
$book_node = Node::load($book['bid']);
foreach ($tokens as $name => $original) {
switch ($name) {
case 'root':
case 'title':
$replacements[$original] = $book_node
->getTitle();
break;
case 'parent':
if (!empty($book['pid'])) {
$parent_node = Node::load($book['pid']);
$replacements[$original] = $parent_node
->getTitle();
}
break;
case 'parents':
if ($parents = token_book_load_all_parents($book)) {
$replacements[$original] = token_render_array($parents, $options);
}
break;
}
}
if ($book_tokens = \Drupal::token()
->findWithPrefix($tokens, 'author')) {
$replacements += \Drupal::token()
->generate('user', $book_tokens, [
'user' => $book_node
->getOwner(),
], $options, $bubbleable_metadata);
}
if ($book_tokens = \Drupal::token()
->findWithPrefix($tokens, 'root')) {
$replacements += \Drupal::token()
->generate('node', $book_tokens, [
'node' => $book_node,
], $options, $bubbleable_metadata);
}
if (!empty($book['pid']) && ($book_tokens = \Drupal::token()
->findWithPrefix($tokens, 'parent'))) {
$parent_node = Node::load($book['pid']);
$replacements += \Drupal::token()
->generate('node', $book_tokens, [
'node' => $parent_node,
], $options, $bubbleable_metadata);
}
if ($book_tokens = \Drupal::token()
->findWithPrefix($tokens, 'parents')) {
$parents = token_book_load_all_parents($book);
$replacements += \Drupal::token()
->generate('array', $book_tokens, [
'array' => $parents,
], $options, $bubbleable_metadata);
}
}
}
return $replacements;
}