7

I want to show a "Join Now" tab on the main menu for anonymous users only.

In the past I used Menu per Role module which does exactly that.

Since it's only one tab I would rather use 5 or 10 lines of code. Which hook should I use and what are the functions to check the current user role and add a menu item (or hide it for logged in users)?

  • 1
    turns out that when you create a menu item with the path "user/register" Drupal will automatically hide it once you are logged in. Sweeeeeeeeet – uwe Apr 14 '12 at 23:21
  • 1
    Please either accept an answer or answer the question yourself and accept it. Leaving the question unanswered means a lot of people will come to check on it only to realize there's nothing left to do here. – Alex Weber Apr 15 '12 at 4:26
10

I think the simplest way would be to override the access callback for the path that your menu link will be pointing to. You can do this in a custom module with hook_menu_alter(), and use user_is_anonymous() as the callback function:

function MYMODULE_menu_alter(&$items) {
  $items['path/to/page']['access callback'] = 'user_is_anonymous';
}

If you add a menu link for this path, Drupal's menu system will respect the access callback, and the link won't be shown to anyone who's logged in.

Using this method will also make sure that logged in users can't access the page directly, which is probably a good thing.

7
  1. Create a view and allow anonymous only to view it
  2. Use the node access module to set permissions
  3. Create a new content type for content only viewable by anonymous users. set anonymous as the only ones with permission to view that content.
  4. Create a custom module for just that page.

3 is my chosen approach. I like the idea of have the type in place should I need additional non member content in the future.

  • btw doing it this way auto hides and shows the content in your menus based on permission – Aaron Ortega Apr 15 '12 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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