All pastes #167448 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-09-12 00:33 UTC
rendered paste body
<?php
// template.php
function _phptemplate_variables($hook, $vars = array()) {
	switch ($hook) {
		case 'node':
			// Create variables for each vocabulary
			$node = $vars['node'];
			$nid = $node->nid;
			naf_set_default_menu($node); // this works.
...
		case 'page':
		  naf_set_default_menu(); // this doesn't. :(
...
}

/**
 * Provides the relationship between node type and active menu on the site
 */
function naf_map_node_type_path() {
  return array(
    'content-articleop_ed' => 'publications',
    'content-bio_pages' => 'about',
...
    'default' => 'about',
  );
}

/**
 * Set the current active menu, based on paths defined in
 * naf_map_node_type_path().
 *
 * @param $node
 *   If this is a node, pass in the node. Otherwise, it's a normal page.
 */
function naf_set_default_menu($node = NULL) {
  $path = NULL;

  $naf_paths = naf_map_node_type_path();

  if ($node) {
    // Set active menu based on node type.
    if (isset($naf_paths[$node->type])) {
      $path = drupal_get_normal_path($naf_paths[$node->type]);
    }
  }
  elseif (menu_get_active_nontask_item() < 0) {
    // No dice. :(
    $path = drupal_get_normal_path($naf_paths['default']);
  }

  // Set active menu item based on node type; using code from
  // http://www.angrydonuts.com/menu_set_location_can_eat_my_sho
  if ($path) {
    global $_menu;
    $mid = $_menu['items'][menu_get_active_item()]['pid'];
    $pid = $_menu['path index'][$path];
    $_menu['items'][$mid]['pid'] = $pid;
    menu_set_active_item($_GET['q']);
  }
}