'How do I make certain menu items visible to certain roles?

In /drupal/admin/structure/menu/manage/main-menu, I have some links that I only want displayed for user's that have a specific permission.

How would I go about this?

In my module, I have

...

$items['resume/joblist'] = array(
  'page callback' => 'ac_resume_job_list',
  'access arguments' => array('view joblist'),
  'title' => 'Job List',
  'description' => 'Job List',
);

...

function ac_resume_permission()
{
  return array("view joblist" => array("title" => "View Job List"));
}

When I go to "resume/joblist" under a user without the permission, I get the "Access Denied" as expected, however the link is still displayed. enter image description here



Solution 1:[1]

hook_perm() was renamed to hook_permission() in Drupal 7, and there's a bit of a mismatch between view mylink that you define and view joblist that you declare as a permission.

You could change your code to look more like this:

function mymodule_menu() {
  $items['mylink'] = array(
    'page callback' => 'mymodule_mylink',
    'access arguments' => array('view mylink'),
    'title' => 'My Link',
    'description' => 'My Link',
  );

  return $items;
}

function mymodule_permission() {
  return array(
    'view mylink' => array(
      'Title' => 'View My Link'
    )
  );
}

After you clear Drupal's cache navigate to admin/people/permissions and assign your new permission to the role you want to be able to access the page you define in hook_menu().

Once you've done that users with that role will be able to access the page :)

Solution 2:[2]

I recommend using the following module: Menu Item Visibility, it does exactly what you need.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Clive
Solution 2 bmunslow