views:

45

answers:

2

hi,

I need to change the menu links on my website (and leave the same items names) depending on the user is a guest, or authenticated user.

What's the standard way to do it ?

thanks

+4  A: 

You cannot dynamically change a menu item's path, because menu items are cached.

Still, AFAIK, there are two ways to get what you want. Both methods require you to create your menu items with hook_menu in a custom module (not from the Menu UI).

The first method is to create two menu items with identical names and set the access rules so that one is only available for logged guests, the other for authenticated users. Since Drupal will only show menu items that the user is allowed to access, only one will show up at any given moment. In Drupal core, you can see how the user module creates a menu item for anonymous users by looking at the /user/login path in user_menu().

The second method is to create a single menu item and check in the menu callback if the user is logged in. If the user is logged in, you serve one page, if not you serve another. In Drupal core, the /user path works like this. See user_page to see how the code works.

marcvangend
I've actually solved by redirecting the user to the correct page without involving menus.
Patrick
Good to see that you have solved it, but if I may ask: where did you put the code that redirects the visitor to the right url? In case you put PHP in a node, I want to show you this article (http://2bits.com/articles/free-your-content-php-moving-php-code-out-blocks-views-and-nodes.html) because there are some disadvantages to that technique.
marcvangend
Creating a node using the PHP input format should only used as temporary solution. Even on Drupal.org, all the pages that needs a dynamic output are generated by a custom module (drupalorg.module) that contains all the customizations necessary for Drupal.org.
kiamlaluno
+2  A: 

Instead of altering the link, you coudl create the menues twice: once with the links for regular users and once with the links for registered/admin/... users

You can put a menu into a block and set it to only allow registered users to see the one block and non-registered users the other block. Either by selecting the proper radio button from the Drupal menu within the block creation form or via PHP that will evaluate and depending on it's return value (TRUE/FALSE) displays it. I suggest to go with the first approach.

DrColossos

related questions