Skip to content

Instantly share code, notes, and snippets.

@leymannx
Created February 16, 2016 15:54
Show Gist options
  • Save leymannx/bcbf563b93e693b8ffc1 to your computer and use it in GitHub Desktop.
Save leymannx/bcbf563b93e693b8ffc1 to your computer and use it in GitHub Desktop.
Drupal hook menu user role access argument
/**
* Implements hook_menu().
*/
function user_has_role_menu() {
$items['admin/mypage'] = array(
'page callback' => 'user_has_role_page_content',
'access callback' => '_user_has_role',
'access arguments' => array(array('editor')),
);
return $items;
}
/**
* @param $roles
* @return bool
*/
function _user_has_role($roles) {
if (user_is_logged_in()) {
global $user;
if (in_array('administrator', $user->roles)) {
return TRUE;
}
foreach ($roles as $role) {
if (in_array($role, $user->roles)) {
return TRUE;
}
}
}
else {
return FALSE;
}
}
/**
* @return mixed
*/
function user_has_role_page_content() {
$content['hello'] = array(
'#prefix' => '<div>',
'#markup' => t('Looky looky'),
'#suffix' => '</div>',
);
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment