This answer led to a discussion about the best way to redirect a user to a specific page if they shouldn't have access to the page they are trying to access.
One option is to set the access callback to true and then redirect users in the page callback. Whilst this seems valid I think it is mixing access functionality with page building functionality in the page callback.
E.g.
function hook_menu() {
$items['player/my_page'] = array(
'title' => t('My Page'), // note this is a required parameter
'access callback' => TRUE,
'page callback' => 'some_function',
);
return $items;
}
function some_function() {
global $user;
if(!$user->uid) { // here checking if the user is logged in but could be checking for a specific permission or field value
$dest = drupal_get_destination();
drupal_goto('user/login', $dest); // this remembers where the user is coming from
}
// carry on building rest of page
}
Another option is to set the access callback function to call a function that checks if the user has access, but then instead of returning false it would redirect the user to another page. This is good as it is separating access logic and page building logic. However the purpose of an access callback is to return a boolean value, so this is breaking that logic by redirecting the user.
E.g.
function hook_menu() {
$items['player/my_page'] = array(
'title' => t('My Page'), // note this is a required parameter
'access callback' => 'check_access',
'page callback' => 'some_function',
);
return $items;
}
function check_access() {
global $user;
// here checking if the user is logged in but could be checking for a specific permission or field value
if(!$user->uid) {
$dest = drupal_get_destination();
drupal_goto('user/login', $dest);
}
return TRUE;
}
Are there any undesirable effects from redirecting users in the access callback that I'm not aware of?
What do you think is best practice here?