Created
February 16, 2016 15:54
-
-
Save leymannx/bcbf563b93e693b8ffc1 to your computer and use it in GitHub Desktop.
Drupal hook menu user role access argument
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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