You are here

function menu_token_values in Token 6

Implements hook_token_values() on behalf of menu.module.

File

./token_node.inc, line 205
Implementations of token module hooks for the core node and book modules.

Code

function menu_token_values($type, $object = NULL, $options = array()) {
  static $menus;
  $values = array();

  // Statically cache menu_get_menus() since this causes a database query
  // every time it is called, and is not likely to change in the same page
  // request.
  if (!isset($menus)) {
    $menus = menu_get_menus();
  }
  if ($type == 'node' && !empty($object)) {
    $node = $object;

    // Initialize tokens to empty strings.
    $values['menu'] = '';
    $values['menu-raw'] = '';
    $values['menupath'] = '';
    $values['menupath-raw'] = '';
    $values['menu-link-title'] = '';
    $values['menu-link-title-raw'] = '';
    $values['menu-link-mlid'] = '';
    $values['menu-link-plid'] = '';
    $values['menu-link-parent-path'] = '';
    $values['menu-link-parent-path-raw'] = '';
    if (!isset($node->menu)) {

      // We need to clone the node as menu_nodeapi($node, 'prepare') may cause data loss.
      // @see http://drupal.org/node/1317926
      $node = drupal_clone($node);

      // Nodes do not have their menu links loaded via menu_nodeapi($node, 'load').
      menu_nodeapi($node, 'prepare');
    }

    // Now get the menu related information.
    if (!empty($node->menu['mlid'])) {
      $link = token_menu_link_load($node->menu['mlid']);
      if (isset($menus[$link['menu_name']])) {
        $values['menu-raw'] = $menus[$link['menu_name']];
        $values['menu'] = check_plain($values['menu-raw']);
      }
      $parents = token_menu_link_get_parents_all($link['mlid']);
      $trail_raw = array();
      foreach ($parents as $parent) {
        $trail_raw[] = $parent['title'];
      }
      $trail = array_map('check_plain', $trail_raw);
      $values['menupath'] = !empty($options['pathauto']) ? $trail : implode('/', $trail);
      $values['menupath-raw'] = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);
      $values['menu-link-title'] = check_plain($link['title']);
      $values['menu-link-title-raw'] = $link['title'];
      $values['menu-link-mlid'] = $link['mlid'];
      if (!empty($link['plid']) && ($parent = token_menu_link_load($link['plid']))) {
        $values['menu-link-plid'] = $parent['mlid'];
        $values['menu-link-parent-path-raw'] = drupal_get_path_alias($parent['href']);
        $values['menu-link-parent-path'] = check_plain($values['menu-link-parent-path-raw']);
      }
    }
  }
  return $values;
}