hook_menu and multiple access arguments in Drupal

Let's take a look at adding multiple access arguments to a Drupal menu item. If you have a specific user permission already in place it might look something like:

$items['admin/store/settings/products/edit/features'] = array(
'title' => 'Product features',
'page callback' => 'drupal_get_form',
'page arguments' => array('uc_product_feature_settings_form'),
'access arguments' => array('administer product features'),)");
...

Unfortunately you can't just add a comma and another access argument to the access arguments array. So, what's next? We need to create a custom function that describes the access arguments. In this example let's call the function: administer_product_features_access. We can direct Drupal to this function by adding an 'access callback' as shown below:

$items['admin/store/settings/products/edit/features'] = array(
'title' => 'Product features',
'page callback' => 'drupal_get_form',
'page arguments' => array('uc_product_feature_settings_form'),
'access callback' => 'administer_product_features_access',
'access arguments' => array(1),
...

Then we just need a function with the multiple access arguments or our custom access code.

function administer_product_features_access()
{
return user_access('administer product features 1') &&
user_access('administer product features 2');
}

Well that's how you do it! I need to get some sleep but hopefully I can check to make sure it's bug free sometime in the near future. Adios.
0 Responses