views:

1258

answers:

5

I need to create a menu item, which changes its title and link if a user has a certain condition, or not. Drupal caches all the menues, so i can't really think of a way to do that.

For example, user has a node attached to his profile, menu item is "Create blabla" (link node/add/blabla) User doesn't have the node, menu item is "Create notblablabla" (link node/add/notblabla)

A: 
  • A simple way to do this, would be to use JavaScript. You can alter HTML without much efford using jQuery. This will require that your users has JS enabled, so it's not a perfect solution.

  • Another option would be to have a single menu item, that linked to a url you created in a module. He you could do the condition check a redirect the user to whichever url he should be redirected to. The only problem with this method would be that the changing the title of the menu item. But you might be able to give a fitting description for both cases. You could also use JS to change the name of the link. That way you would keep the functionality intact without JS but improve the UI for users with it enabled.

googletorp
+1  A: 

Drupal does not allow for dynamic menu items, but it can hide certain menu items if the user is not allowed to go there. Referring to your example, if you create both the links and use the permission system to restrict the creation of those node types to certain roles, Drupal will only show the menu items if the user has the required role. Maybe that helps in your situation.

Other options are:

  • write a simple module that shows a single link for all users and redirects to the appropriate page when clicked
  • create a custom block which displays the correct link based on the current user (make sure the block is not cached)
  • use javascript like googletorp suggest (although I wouldn't recommend it for the reasons he mentions)
marcvangend
Since it's a condition that needs to be met, permissions will probably not help.Creating a block will remove the link from the menu system, so it wouldn't be a menu item anymore.
googletorp
True, a link in a block is not a menu item, but in some cases that is not a problem so that's why I still wanted to mention it as an option.
marcvangend
A: 

I would probably create two menu items, and use the theme system to hide one or the other based on the condition.

It's been a while, but I'd look at:

sprugman
A: 

Since this menu item seems to be predicated on user profile information, I would suggest writing a simple module that implements hook_menu_alter() to alter the menu based on your condition. You can then call menu_cache_clear() inside hook_user() to renew the menu cache when the user profile changes.

emmychan
A: 
dazweeja