By mastermindg on
I had originally created a block with PHP code that displayed Welcome/Logout or Login/Register depending on whether or not a user was logged in. I wanted to package this with my module for safe-keeping, so I built a little function that does just that. I wanted to share this with the community. Here it is:
function _example_custom_user_menu() {
global $user;
$host = 'http://' . $_SERVER['SERVER_NAME'];
$opentag = '<ul class=inline><li class=first>';
if ($user->uid != 0) {
$welcome = 'Welcome '."<a href='$host/user/$user->uid'>".$user->name."</a></li><li><a href='$host/user/logout'>Logout</a></li>";
}
else {
$welcome = "<a href='$host/user'>Login</a></li><li><a href='$host/user/register'>Register</a>";
}
$closetag = '</li></ul>';
$content = join(array($opentag,$welcome,$closetag));
$block['content'] = $content;
return $block;
}This can be called directly by hook_block_view:
function example_block_view($delta) {
$block = array();
switch ($delta) {
case 'custom_user_menu':
$block = _example_custom_user_menu();
break;
}
return $block;
}Let me know if there are any ways to refine my code. I'm always looking to improve!
Comments
I realize you marked this as
I realize you marked this as solved but the code can be simplified to
[Edited to fix welcome message]
I added a line and corrected
I added a line and corrected the "Welcome" link:
## Note: The above code is fictitious. Any resemblance to real or actual working code is purely coincidental.
Thanks for that! I was
Thanks for that! I was wondering how to include the t() function.
## Note: The above code is fictitious. Any resemblance to real or actual working code is purely coincidental.