0

I have 3 roles on site:

  1. anonymous user
  2. authenticated user
  3. admin

I created a new page:

function learning_menu()
{
    $items = array();

    $items['learning'] = array(
        'page callback'    => 'learning_output',
        'access callback' => 'learning_access',
        'type' => MENU_CALLBACK,
        );

    return $items;
}

I manually created a menu item for this page in the top menu. This item should be displayed only for authenticated users, but not admins. Unfortunately, admin as also an authenticated user. So I had to use a trick to disallow access to this page to admins using this access callback:

function learning_access()
{
    global $user;

    $n = count($user->roles);
    if ($n === 1)
    {
        $data = array_values($user->roles);
        if ($data[0] === 'authenticated user')
        {
            return true;
        }
    }

    return false;
}

It works but when I uploaded this module to server, I was unable to manually add a menu item for this page. Because admin doesn't have access to it.

How can I allow an admin to access this page? In the same time the menu item shouldn't be shown for him.

  • Then you can do another thing, remove 'access callback', but in the page callback function write your condition and make drupal_access_denied for admin user – NTT Aug 25 '16 at 13:12
  • @Nagarjuna In this case the menu item will be shown. – user4035 Aug 25 '16 at 13:18
  • 1
    hmm yaa.. but this some thing like deadlock situation. Hide that Menu using drupal.org/project/menu_item_visibility – NTT Aug 25 '16 at 13:26
  • @Nagarjuna This module won't help. Because even if I just tick authenticated user, it will still show for the admin. Because admin is an authenticated user. – user4035 Aug 25 '16 at 15:25
  • @Nagarjuna I think, the only way is to attach a hook to the function that generates menu and check this item there. – user4035 Aug 25 '16 at 15:26
1

As per the discussion above,

Remove 'access callback' => 'learning_access', add drupal_access_denied() in page call back

function learning_menu() {
  $items = array();

  $items['learning'] = array(
    'page callback' => 'learning_output',
    'type' => MENU_CALLBACK,
  );

  return $items;
}



function learning_output() {
  global $user;

  $n = count($user->roles);
  if ($n === 1) {
    $data = array_values($user->roles);
    if ($data[0] === 'authenticated user') {
      drupal_access_denied();
    } else {
      //YOUR code Here      
    }
  }
}

And Use Roles for Menu module to hide the learning menu.

This module allows you to restrict access to menu items based on user roles. It is used when you don't want to copy your whole menu just because for one specific role you want to hide one menu item.

The module will add a fieldset with options for restricting access or gaining access to menu link for a specific role/roles

  • No need to add access_denied into the page callback. I added 'access arguments' => array('access content'), into the menu item. – user4035 Aug 26 '16 at 9:38
  • Cool Cheers. :) – NTT Aug 26 '16 at 9:39

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.