Tag Archives: drupal menu

Permissions and roles in Drupal 7

First of all you need to define ‘access callback’ and ‘access argument’ in hook_menu() for respective uri. like this:
function hook_menu()
{
     $items = array();
     $items[item/list] = array(
          ‘access callback’ => ‘item_access’,
          ‘access arguments’ => array(‘view’),
     );
     return $items;
}
Then you need to create hook_permission method within in the module
function hook_permission()
{
     return array(
          ‘view item’ => array(
               ‘title’ => t(‘View Item’),
               ‘description’ => t(‘User can view Item.’),
               ‘restrict access’ => true,
          )
     );
}
Then you need to define item_access method
 
function item_access($op, $item = null, $account = null)
{
     global $user;
if($account){
     $user = $account;
}
if($op == ‘view’){
     return user_access(‘view item’, $user);
}
return false;
}
Now clear your cache. Donot forget to clear your cache as drupal 7 makes heavy usage of its cache.
 
Now go to /people/permissions page of your admin panel.
 
There you should see ‘View Item’ with little checkboxs, now enable then view of item for any users by selecting on them.

Copyright © 2020. Powered by WordPress & Romangie Theme.