What is the difference between the access arguments and access callbacks in Drupal?

Can you tell me the differences between access arguments and access callbacks?

Martha Butler Asked on October 21, 2015 in Drupal.
Add Comment
1 Answer(s)
Best answer

The “Access callback” is the function that is used to verify whether he user has access to the menu callback. As special case, it could be the value TRUE, and in which case all  of the users would have access to the menu callback. What that means is that verifying the access permission would be bypassed/skipped.
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 event that it’s a number, then the value will be replaced with a value taken from the menu path. If you are looking to avoid this replacement, instead of a number you will need to use a string – for example, using “1” as one for the arguments passed to the access callback would avoid this automatic replacement.

Here is an example where the access callback is a function 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,
);

And here is an example for where the access callback is not a function:

$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 example 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”.  For this particular case, 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’,
);

Lets say we use the example above where we have a menu defined and the callback is invoked with a path like “user/hello.”  Then 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, which in this case will make the value to be “hello.”

Kevin Cane Answered on October 21, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.