0

Using Structures/Menus/User Menu in DRUPAL 7 i am adding a custom menu link under it and providing a path to it as node/whatever_my_nid. It is working fine. I have cleared all cache. However this menu link is being displayed for all users. I want it to be displayed only for authenticated users and admin. This should be a simple thing to figure out but apparently i cannot. I thought it would have been a permission issues but i don't think so. Would appreciate any help. Thanks

Edit: I found that this code may help:

<?php
global $user;
if ($user->uid) {
if ($menu = theme_menu_tree()) {
   $menu = "<div class=\"menu\">". $menu . "</div>";
   return $menu;
}
}
else {return;}
?>

But where do i place this code...In my page.tpl.php or somewhere else like in template.php in my themes?

I tried placing the above function in my theme's preprocess function as:

function ypwb_preprocess_block(&$variables, $hook) {
     if ($user->uid) {
        if ($menu = theme_menu_tree()) {
           $menu = "<div class=\"menu\">". $menu . "</div>";
           return $menu;
        }
      }
        else {return;}
    }

However, the menu is still showing for anonymous user as well.

  • 2
    "displayed only for authenticated and anonymous users". That's everyone. – Letharion Feb 22 '13 at 13:32
  • @Letharion oh...my bad...i meant only authenticated and admi..have edited now – why Feb 22 '13 at 13:33
  • 1
    Menu per role was written exactly for this usecase. – AjitS Feb 22 '13 at 13:35
  • @Indrock yes but its still in dev version for Drupal7 – why Feb 22 '13 at 13:36
  • @kewal I'm using the module on a site (in production) since 4 months and it just works fine for me. No issues :-) – AjitS Feb 22 '13 at 13:41
3

What I usually do is:

  1. Install and enable Taxonomy Access
  2. Create a vocabulary called Access
  3. Create 2 terms in the new Access vocabulary: Public and Private
  4. Attach the term reference fields for Access vocabulary to each node type you wish to protect. You may want to set the default value to "Public".
  5. Go to Taxonomy Access configuration page and set D (deny) View access to Anonymous users on Private term. Leave the rest of the options (update delete view tag etc) I (ignore).
  6. Configure the access control for Authenticated users and set A (allow) View access to Authenticated users on Private term, and set I (ignore) for all else.
  7. Now all you need to do is set Private on nodes you wish to protect, and set Public for all else.
1

thanks to @Indrock, i got a very simple and effective answer Menu per role is the way to do so.

This module allows you to restrict access to menu items based on user roles. It depends on the Drupal core menu.module—just activate both modules and edit a menu item as usual. There will be a new fieldset that allows you to restrict access by role. The fieldset also appears in the node edit form.

  • If you have XML sitemap on your website you're not really protecting that node, people can still access it from a search engine/copying the link from your sitemap. It's not really an effective solution imho. – Beebee Feb 22 '13 at 14:14
  • hmm...that is a point to be considered....so what do you suggest – why Feb 22 '13 at 14:30
  • I suggest using one of the Access Control modules like Content Access or Taxonomy Access or there's a whole list of them including the above 2 I mentioned on the Menu Per Role module's page. – Beebee Feb 22 '13 at 14:33
0

If you are putting it in a block, then create 2 menus.

Then create 2 menu blocks, linking to these 2 menus.

Restrict the 'logged in' version to authenticated roles and the anonymous user menu block to the anonymous user role.

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.