You are here

function book_tokens in Token 7

Same name and namespace in other branches
  1. 8 token.tokens.inc \book_tokens()

Implements hook_tokens() on behalf of book.module.

File

./token.tokens.inc, line 1005
Token callbacks for the token module.

Code

function book_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  // Node tokens.
  if ($type == 'node' && !empty($data['node'])) {
    $node = $data['node'];
    if (!empty($node->book['mlid'])) {
      $link = token_book_link_load($node->book['mlid']);
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'book':
            $replacements[$original] = $sanitize ? check_plain($link['title']) : $link['title'];
            break;
        }
      }

      // Chained token relationships.
      if ($book_tokens = token_find_with_prefix($tokens, 'book')) {
        $replacements += token_generate('menu-link', $book_tokens, array(
          'menu-link' => $link,
        ), $options);
      }
    }
  }
  return $replacements;
}