You are here

function menu_views_tokens in Menu Views 7

Same name and namespace in other branches
  1. 8.3 menu_views.module \menu_views_tokens()
  2. 7.2 menu_views.module \menu_views_tokens()

Implements hook_tokens().

File

includes/tokens.inc, line 28
Token hooks for the menu_views module.

Code

function menu_views_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  $node = FALSE;
  $parent = FALSE;

  // Menu link tokens.
  if ($type == 'menu-link' && !empty($data['menu-link'])) {
    $link = (array) $data['menu-link'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'node':
          $node = menu_get_object('node', 1, $link['link_path']);
          if ($node && $node->nid) {
            $title = $node->title;
            $replacements[$original] = $sanitize ? filter_xss($title) : $title;
          }
          else {
            $replacements[$original] = NULL;
          }
          break;
        case 'parent':
          if (isset($link['plid']) && !empty($link['plid']) && ($parent = menu_link_load($link['plid'])) && ($node = menu_get_object('node', 1, $parent['link_path'])) && $node->nid) {
            $title = $node->title;
            $replacements[$original] = $sanitize ? filter_xss($title) : $title;
          }
          else {
            $replacements[$original] = NULL;
          }
          break;
      }
    }

    // Chained token relationships.
    if (($node_tokens = token_find_with_prefix($tokens, 'node')) && ($node = menu_get_object('node', 1, $link['link_path']))) {
      $replacements += token_generate('node', $node_tokens, array(
        'node' => $node,
      ), $options);
    }
    if (($parent_tokens = token_find_with_prefix($tokens, 'parent')) && isset($link['plid']) && !empty($link['plid']) && ($parent = menu_link_load($link['plid'])) && ($node = menu_get_object('node', 1, $parent['link_path'])) && $node->nid) {
      $replacements += token_generate('node', $parent_tokens, array(
        'node' => $node,
      ), $options);
    }
  }
  return $replacements;
}