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

  global $user;

   $list = array();

   if ( user_is_logged_in() ) {
    $list[] = 'Welcome ' . l($user->name, 'user/' . $user->uid);
  }
  else {
    $list[] = l(t('Login'), 'user');
    $list[] = l(t('Register'), 'user/register');
  }
  $options = array(
    'items' => $list,
    'type' => 'ul',
    'attributes' => array(
      'class' => 'inline'
    ),
  );
  $block['content'] = theme('item_list', $options);
  return $block;

[Edited to fix welcome message]

I added a line and corrected

I added a line and corrected the "Welcome" link:

    $list[] = 'Welcome ' . l($user->name, 'user/'.$user->uid);
    $list[] = l(t('Logout'), 'user/logout');

## 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.

nobody click here