21

What's the difference between access arguments and access callback?

Suppose I set the access callback to 'user_access', and array('custom permission') is the value for the access arguments.

What does that mean? How do they affect each other?

27

"Access callback" is the function that is called to verify the user has access to the page. As special case, it could be the value TRUE, and in that case all users would have access to it; in other words, the access permission would be bypassed.

In the case you are using a function name for the "access callback" value (by default, it's "user_access"), then you can also use "access arguments", which is an array containing the arguments passed to the access callback function.
As with other menu callbacks, the arguments must be a string, or a number; in the case it's a number, the value will be replaced with a value taken from the menu path. If you want to avoid this replacement, instead of a number you need to use a string; for example, using "1" as one for the arguments passed to the access callback would avoid the automatic replacement.

These are some example of menu callback declarations used from Drupal core modules. (The examples are from Drupal 7 code, but for what I want to point out, that doesn't make any difference.)

This is an example where the access callback is user_access().

  $items['file/progress'] = array(
    'page callback' => 'file_ajax_progress', 
    'delivery callback' => 'ajax_deliver', 
    'access arguments' => array('access content'), 
    'theme callback' => 'ajax_base_page_theme', 
    'type' => MENU_CALLBACK,
  );

This is an example where the access callback is not a function name.

  $items['user'] = array(
    'title' => 'User account', 
    'title callback' => 'user_menu_title', 
    'page callback' => 'user_page', 
    'access callback' => TRUE, 
    'file' => 'user.pages.inc', 
    'weight' => -10, 
    'menu_name' => 'user-menu',
  );

In this case, the access callback is user_view_access() to which is passed, not the number 1, but a value taken from the menu path (which in this case is "user/%user"); this is a particular case, as the function will get the value returned by user_load().

  $items['user/%user'] = array(
    'title' => 'My account', 
    'title callback' => 'user_page_title', 
    'title arguments' => array(1), 
    'page callback' => 'user_view_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1),
    // By assigning a different menu name, this item (and all registered child
    // paths) are no longer considered as children of 'user'. When accessing the
    // user account pages, the preferred menu link that is used to build the
    // active trail (breadcrumb) will be found in this menu (unless there is
    // more specific link), so the link to 'user' will not be in the breadcrumb. 
    'menu_name' => 'navigation',
  );

Suppose the previous menu were defined as follows, and invoked with a path like "user/hello."

  $items['user/%'] = array(
    'title' => 'My account', 
    'title callback' => 'user_page_title', 
    'title arguments' => array(1), 
    'page callback' => 'user_view_page', 
    'page arguments' => array(1), 
    'access callback' => 'user_view_access', 
    'access arguments' => array(1),
    // By assigning a different menu name, this item (and all registered child
    // paths) are no longer considered as children of 'user'. When accessing the
    // user account pages, the preferred menu link that is used to build the
    // active trail (breadcrumb) will be found in this menu (unless there is
    // more specific link), so the link to 'user' will not be in the breadcrumb. 
    'menu_name' => 'navigation',
  );

In this case, the access callback would have received as argument the value taken from the path (0 means "user," and 1 means the part after "user" and the slash); in this case, that value is "hello."

To better understand these wildcard arguments, see Wildcard Loader Arguments. The documentation page is tagged Drupal 6, but what reported is still valid for Drupal 7.

  • awesome explanation. In my requirement some custom button say "vote now" should be processed only by authenticated user but button should be shown to anonymous too and when anonymous user click that he should redirect to login page and process continues. For this particular scenario is there special access condition to show button to anonymous?. Commenting here because it would make this post more complete wiki. Bookmarked..:) – kiranking Apr 10 '15 at 17:32
4

Access callback is a function that examines if some user has some permissions. The default access callback is user_access()

Access arguments lists permission those are examined by access callback. e.g. "access contents"

  • What else might be a value for access callback? If it were something other than user_access(), would that obviate the need for access arguments? – user1359 Jun 27 '11 at 16:09
  • 2
    Whatever you want. Any function that returns TRUE or FALSE. – Berdir Jun 27 '11 at 16:33
  • So then you wouldn't need access arguments? – user1359 Jun 27 '11 at 16:45
  • Yes then you can skip it – Shoaib Nawaz Jun 27 '11 at 17:20

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.