Issues with removing tabs using hook_menu_alter

alohaglide - September 18, 2009 - 03:34

Aloha,

I have a view that presents as a Menu tab.

I have created a module to check and see if the view contains any items. If there are no items, I would like the tab to be hidden.

Using the instructions here ( http://drupal.org/node/483324 ) I have used hook_menu_alter to try and make the tab disappear.

It doesn't seem to be working. I was wondering if someone could provide some guidance. Is there something different that needs to be done removing menu tabs created from a view?

I have tried various versions of the following code:

<?php
function MyModule_menu_alter(&$items) {
 
//$items['node/%node/documents']['type'] = MENU_CALLBACK;
  //unset($items['node/%node/documents']);
   
  //if ($item['link_path'] == 'node/247/documents') {
  //    $item['hidden'] = 1;
  //}
}
?>

many thanks!!

Yeah, two things: 1. Views

John Morahan - September 18, 2009 - 12:07

Yeah, two things:

1. Views doesn't use %node but %views_arg
2. Views itself uses hook_menu_alter() so you may need to increase your module's weight to ensure it runs after.

However. If you want to remove the tab dynamically - i.e. display it sometimes but not always - then setting the type to MENU_CALLBACK won't work. Instead, change the access callback to a function you control, and conditionally return TRUE or FALSE depending on whether you want the tab displayed or not.

Increasing the module weight

allenshaw - October 22, 2009 - 21:49

Increasing the module weight above Views' weight was the key here for me. Had to do that via sql in the {system} table directly (couldn't find a feature in Drupal to do it through the UI).

Thanks John Morahan for the tip!

menu_alter vs menu_link_alter

rschwab - October 15, 2009 - 19:48

I've been struggling with these functions myself, and am starting to think they're just broken.

One problem you will have with the code above is trying to set the Hidden field based on the link_path field. These are in the menu_links table, which means they can only be changed with the function hook_menu_link_alter. The menu_router table is changed by function hook_menu_alter.

But again, I'm not expert because I can't get these functions to work either as evidenced by my despair here: http://drupal.org/node/604850

- Ryan

Try unsetting that variable

Jay Matwichuk - October 23, 2009 - 02:08

After you use hook_menu_alter, you need to clear your cache before the changes are reflected. Have you done this?

Yes, I actually solved that

rschwab - October 23, 2009 - 16:10

Yes, I actually solved that problem I was having (click on my link up there to see it worked through). It had to do with the wildcard in the paths I was using. The function that those wildcards load automatically set different parameters of your menu item.

Thanks for all the help you provide here Jay, it is much appreciated.

- Ryam

 
 

Drupal is a registered trademark of Dries Buytaert.