Last updated April 2, 2010. Created by wgrunberg on September 22, 2009.
Log in to edit this page.

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.

<?php
  $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;
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

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?

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!
}

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;
?>

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 @ http://wunderkraut.com/

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!

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; }
?>

Another solution is using context module :-)

Works like a charm.

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

Here's the drupal 7 solution for this

<?php
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

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