Programmatically Display/Change Menu Link
When an anonymous user comes to the site, the last item in the Primary Links needs to say Login. However, if they are logged in, it needs to say Account.
Whats the easiest way t开发者_C百科o achieve this in code?
I accomplish this by using the me aliases module to generate one single path for the account page of the logged in user (e.g. user/me
) and adding both user/me
and user/login
to the menu.
If you wanted to do this programmatically, you'd use menu_link_save()
:
$account = array(
'link_path' => 'user/me',
'link_title' => t('Account'),
'weight' => 100,
);
$login = array(
'link_path' => 'user/login',
'link_title'=> t('Login'),
'weight' => 100,
);
menu_link_save($account);
menu_link_save($login);
Due to the way Drupal handles menu permissions, a logged in user will not see Login (logged in users do not have access to user/login
) but will see Account, and logged out users will see Login but won't see Account (anonymous users do not have access to user/me
).
精彩评论