Search code examples
phpdrupal-7access-controladministratorhook-menu

Drupal 7 Access Callback Not Working Properly


Drupal 7 Hook_menu access callback is not returning the correct boolean.

Before we begin. YES! Cache is cleared... a lot.

I implemented a simple function for testing:

$items['tutor_review_selection'] = array(
    'title'             => t('example'),
    'page callback'     => 'my_module_example_page',
    'access callback'   => my_module_access( array('administrator') ),
    'type'              => MENU_NORMAL_ITEM
);

function my_module_access( $roles ) {
    global $user;

    $check = array_intersect($roles, array_values($user->roles));

    return empty( $check ) ? FALSE : TRUE;
}

This returns TRUE for logged in and logged out users.

Here is the important part:

I call 'my_module_access' function in 'my_module_example_page' function and it works correctly.

Can anyone shine some light onto why this would not work in access callback?

Maybe something to do with order of operation?

Cache is cleared.


Solution

  • If you check the Drupal 7 hook_menu documentation you will see the following code:

      function mymodule_menu() {
        $items['abc/def'] = array(
          'page callback' => 'mymodule_abc_view',
          'page arguments' => array(1, 'foo'),
        );
        return $items;
      }
    

    'page callback' accepts a string, which is the callback function name. The arguments to be sent to that function are provided in the 'page arguments' array.

    edit Note that you should probably create a permission and assign your roles to that permission, then check the permission instead of checking for specific roles.