in my scenario I am using Drupal 7 with the ubercart modules. I like to add a link in my main menu that is only visible if you are an authenticated user.

For testing purposes I am using the bartik theme.

It is the same function like user/login, which is only visible if you are an anonymus user.

I found some hints and even examples in the drupal api - like


The following implements hook_menu_alter to remove access to the "forum" tab for the anonymous user:

* Implements hook_menu_alter.
* Check access for forum menu item.
function MYMODULE_menu_alter(&$items) {
$items['forum']['access callback'] = '_accesscheck';

* Callback to disallow access for the anonymous user.
function _accesscheck(){
  global $user;
 return $user->uid;

Even after many attempts, I can't catch a value for $items, I was using main_menu.

I like to use the function in the theme template. How can I find the variables that I need to alter one item of my menu - I called it > cart <.

I even tried to print_r($items), but nothing was found.

How can I retrieve $items for the main menue

  • Is the /forum path a View by any chance?
    – Clive
    Apr 17, 2014 at 15:15
  • sorry its not a view, I just copied the example and hoped to find the right value for the main menu. I only extended the main menu using the user interface, created a new link and gave it a name [cart] and a path [cart]. All I want is that the link becomes invisible for guests. Apr 17, 2014 at 15:20
  • That's a good thing, Views paths can be a pain to work with. Usual suspects: Is the module definitely installed? Have you cleared the caches since implementing the hook? (your code looks right to me BTW)
    – Clive
    Apr 17, 2014 at 15:21
  • @FreeRadical I guess support for this one goes over to you then ;)
    – Clive
    Apr 17, 2014 at 15:28

2 Answers 2


hook_menu_alter only gets executed when you clear cache or rebuilding menu. I would suggest if you want to debug the $items variable, What I used to do is do print_r($items) and die() after that. Then I would try to clear the cache from the admin UI or save it into watchdog for debugging.


Please note that in most cases, Drupal will manage menu item visibility and access via menu items just fine by itself. If you link to "something" in the main menu, and access to "something" is limited, Drupal will hide the menu item from those that shall not have access. In other words: In most cases, there is no need for you to do anything. (i.e.: there is no need to use hook_menu_alter, etc.)

In some cases, it doesn't work like that. The example you've copy-pasted from is a remedy for one of these rare case.

I suggest you first try to create the menu link normally in the admin panel, set access control for whatever it is it points to only allow the authenticated user access. Clear caches, and test. If all went well, the menu item will not be visible for the anonymous user when you test it.

And if this is one of those pesky cases where the above don't work, come back here. Edit your question an give us all the details about what you're linking to and how access to that is controlled. Then we need to review other options.

The reason you're unable to "catch" a value for $items by placing a print_r statement inside hook_menu_alter is because this hook is not invoked on page loads. It is only invoked when the menu list is rebuilt. This happens when a module is installed/uninstalled, or when all caches are cleared. However, he output of print_r may be lost if it is output outside of a page load. Instead, to debug $items, install and enable the Devel module and use the following instead of print_r.

dpm($items, '$items');

The output of dpm, unlike print_r is not output immediately, but queued and will be shown on the next page load.

However, in this case, I don't think you will learn what you need to know in order to block access by looking at $items. Instead, just figure out what paths of the menu items you want to block access to. It is not what the menu is called that matters, it is the path it leads to.

  • I set all permission as usually, especially for the cart I only gave permissions to the admin. But this was not enough. Now for testing purposes I activated the forum and and typed down the code for the access limitation. Beside of that I created another link - to a node [ a basic page] and another to the cart link. I used the code like this. function zuser_menu_alter(&$items) { $items['forum']['access callback'] = '_accesscheck'; $items['cart']['access callback'] = '_accesscheck'; $items['impressum']['access callback'] = '_accesscheck';. The limitation is working only for the forum Apr 17, 2014 at 16:40
  • What is the path to the cart landing page and the impressum landing page? Apr 17, 2014 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.