14

Can someone explain "access arguments" in Drupal? Trust me I have tried Googling it but I am just not getting a clear grasp.

apaderno
  • 28,547
  • 16
  • 75
  • 90
user363036
  • 319
  • 1
  • 2
  • 7

3 Answers3

13

in /admin/user/permissions you will see lots of access options. they come from drupal modules, and lets the site administrator distribute specific permissions to user roles (drupal provides 'anonymous' and 'registered' roles by default). modules declare them through hook_perm and they are as easy to use as:

function mymodulename_perm {
return array('use custom feature', 'use the other custom feature');
}

and they will show up there, ready to be used. now, in any function of yours, you can check for user access through user_access which is just as easy to use:

if (user_access('use custom feature')) {
  //do something
}
Capi Etheriel
  • 3,542
  • 28
  • 49
  • 3
    and if you're wondering how to use this on hook_menu, just set 'access arguments' => 'use custom feature' on your menu item. 'access callback' is set to user_access by default, but might be overridden (check http://api.drupal.org/api/function/hook_menu/6 for more details) – Capi Etheriel Jun 10 '10 at 23:06
  • 3
    For Drupal 7 the function is called [hook_permission](https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_permission/7) – AvL Jun 01 '13 at 12:47
  • Hi could you check out my question, its some what similar but I am using drupal 7 http://stackoverflow.com/q/22832569/1877909 – Hitesh Apr 03 '14 at 09:00
9

Access arguments are the arguments passed to the function that checks if a user has access to a menu.

Given a menu callback definition as the following

  $items['blog/feed'] = array(
    'title' => 'RSS feed',
    'page callback' => 'blog_feed',
    'access callback' => 'custom_module_blog_access',
    'access arguments' => array('feed'),
    'type' => MENU_CALLBACK,
  );

The function custom_module_blog_access() will be called as custom_module_blog_access('feed'). If the function returns TRUE, then the user will be given access to the menu callback; differently, the user will see the error 403 page (access denied). Normally, the access callback is not defined, and by default Drupal will use user_access().

apaderno
  • 28,547
  • 16
  • 75
  • 90
  • `'type' => MENU_CALLBACK,` can you tell me more about MENU_CALLBACK ...when and how to use it – Hitesh Apr 03 '14 at 07:02
  • That is a different question. `:)` Read [`hook_menu()`](https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7) first, and ask a question if it is not clear what `MENU_CALLBACK` does. – apaderno Dec 15 '14 at 20:03
  • Thanks for response ;) but already explored on that and found the answer :) – Hitesh Dec 16 '14 at 05:23
0

For using predefined valid permissions, such as system permissions:

  1. Check the list of valid permissions at: /admin/people/permissions

D7 - Permission by Role

  1. Copy the permission string and add it to your 'access arguments' array:

    function mymodule_menu() {
        $items['admin/config/mymodule_config'] = [
            'title' => 'MyModule',
            'page callback' => 'drupal_get_form',
            'access callback' => '_mymodule_admin_form',
            'access arguments' => array('administer site configuration'),
            'type' => MENU_CALLBACK
      ];
    
      return $items;
    }
    

Reference: Valid access arguments

Community
  • 1
  • 1
Eduardo Chongkan
  • 752
  • 7
  • 12