I want to make a different menu between user and guest, here the code that i’m trying
<?php
function my_module_menu() {
if(user_is_logged_in()) {
$items = array();
$items['my_module/user1']=array(
'title' => t('1. User menu 1'),
'page callback' => 'test1',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
'expanded' => TRUE,
);
$items['my_module/user2']=array(
'title' => t('2. User Menu 2'),
'page callback' => 'test2',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
'expanded' => TRUE,
);
}
else{
$items = array();
$items['my_module/guest1']=array(
'title' => t('1. Guest menu 1'),
'page callback' => '1test1',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
'expanded' => TRUE,
);
$items['my_module/user2']=array(
'title' => t('2. Guest menu 2'),
'page callback' => '1test2',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
'expanded' => TRUE,
);
}
return $items;
?>
problem is, the menu view i get only user view (user is logged in) whether user is logged in or not. Is something wrong with my code?
The problem is that
hook_menuis called when the menu is rebuilt, not for every single page load (as this would be very bad for performance).The easiest way to do what you want is to use the
access callbackkey instead ofaccess argumentskey in your menu items to make the on-the-fly decision about whether the user is logged in. As Drupal won’t show the menu items for any user for which thataccess callbackfunction returnsFALSE, you’ll get the effect you want:All of these menu items will be added to the same menu, but only two will ever show at any one time for a user.