Here is a block visibility snippet for showing a block on pages that have a specific active menu.
NOTE: Core Drupal 6 will always return "navigation" as the default active menu name - menu_get_active_menu_name(). You will have to install a module such as Menu Breadcrumb (http://drupal.org/project/menu_breadcrumb) to return one of the other active menus. See #289767: menu_get_active_menu_name() always returns 'navigation' for more information.

I use this snippet to show my multiple menus' blocks only on node pages that have an associated menu entry.
In this example, the block will show up if a page has a menu entry in the "menu-my-menu" menu.
This snippet has been successfully tested in Drupal 6.14.

  $match = FALSE;
 
  // block is visible if the node has the following active menu
  $menu = 'menu-my-menu';
 
  if (menu_get_active_menu_name() == $menu){
    $match = TRUE;
  }
 
  return $match;

D7 version, tested:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $active_trail = menu_get_active_trail();
  end($active_trail);
  $nid = 1; // change to nid of parent node you want to check
  if ($active_trail[1] && $active_trail[1]['link_path'] == 'node/'.$nid) {
    return true;
  } else {
    return false;
  }
}  

Comments

FiNeX’s picture

And what if I need to show the block only if the current page is a child of a first level menu of the specified menu?

charlie-s’s picture

I hope you're ready for an ugly solution. This got me working quickly though.

Install Menu Node API module: http://drupal.org/project/menu_node (this will setup a nice relation table called 'menu_node' that has a simple 2-column table which pairs a mlid [menu link id] with a node id).

You can then throw this query (broken down here for demonstration purposes):

$query = "SELECT mlid FROM menu_node WHERE nid='$node->nid'";
$result = db_query($query);
while ($data = db_fetch_object($result)) {
  $mlid = $data->mlid; // this is the menu link id that we need for the current node
}

$query = "SELECT menu_name FROM menu_links WHERE mlid=$mlid";
$result = db_query($query);
while ($data = db_fetch_object($result)) {
  echo $data->menu_name; // this is the parent menu name for the current node!
}
charlie-s’s picture

The above code is a general outline for the queries, do with it what you wish. Here's a full block of code to use on a block's 'Show if the following PHP code returns TRUE' option. This requires the Menu Node module linked to in my previous post:

<?php 
$var_menu = 'menu-my-custom-menu-name';
$match = FALSE;

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
}

$query = "SELECT mlid FROM menu_node WHERE nid='$nid'";
$result = db_query($query);
while ($data = db_fetch_object($result)) {
  $mlid = $data->mlid;
}

$query = "SELECT menu_name FROM menu_links WHERE mlid=$mlid";
$result = db_query($query);
while ($data = db_fetch_object($result)) {
  $parent_menu = $data->menu_name;
}

if ($parent_menu == $var_menu) {
  $match = TRUE;
}

return $match;
?>
MiSc’s picture

I tried the above solution, but I did not work out for me for some reason, also I wanted to do the selecting of the which menu to choose a little bit dynamic.

So I installed menu node api and hide all of the my menu blocks. Created a new block and put this inside of it:

<?php
if (!drupal_is_front_page()) {

if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  $nid = arg(1);
}

if (strlen($nid)>=1)
{
$query = "SELECT mlid FROM menu_node WHERE nid=$nid";
$result = db_query($query);
while ($data = db_fetch_object($result)) {
$mlid = $data->mlid;
}
$query = "SELECT menu_name FROM menu_links WHERE mlid=$mlid";
$result = db_query($query);
while ($data = db_fetch_object($result)) {
$parent_menu = $data->menu_name;
}
}

if ($parent_menu == 'primary-links')
{
unset($parent_menu);
}

if (strlen($parent_menu)>=3)
{
$block = module_invoke('menu', 'block', 'view', $parent_menu);
print '<h2 class="title">' . $block['subject'] . '</h2>';
print $block['content'];
}
}
?>

Could really need some tweaking, but I did not have that time...

// Mikke Schirén @ https://digitalist.se/

charlie-s’s picture

I agree, we could both combine the sql queries and do something more elegant, but hey -- I have a product to deliver and this does the job well!

Silicon.Valet’s picture

Anything wrong with the somewhat more succinct:

<?php 
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $res = db_query( "SELECT mlid FROM menu_links WHERE link_path = '%s' AND menu_name = 'menu-studentlife'",  "node/".arg(1) );
  return is_array(db_fetch_array($res));
} else { return FALSE; }
?>
FiNeX’s picture

Another solution is using context module :-)

dadderley’s picture

Works like a charm.

Rob T’s picture

Thanks. My block is now menu-aware. Thanks for this menu-based visibility snippet.

EgbertB’s picture

Here's the drupal 7 solution for this

if (arg(0) == 'node' && is_numeric(arg(1))) {
  $result = db_query( "SELECT mlid FROM inet_menu_links WHERE link_path ='node/".arg(1)."' AND menu_name = 'name-of-the-menu'");
  if ( $result->rowCount() >0) {return TRUE;} else {return FALSE;}
} else { return FALSE; }

Egbert - webdevelopment and implementation - www.overboord.nl

addissimo’s picture

How do you determine the name that menu_breadcrumb sets as $menu?