I am replacing a drupal 6 website by a drupal 7 one with the same content. On that Drupal 6 website there is a block that comes visible when you're logged in. As heading it shows the message 'Welcome {username}' in that block.

I'm looking for a way to do that in Drupal 7, i tried to do this with the module 'Token' but i doesn't work. What's the best way to do this?

migrated from stackoverflow.com Jan 30 '12 at 13:33

This question came from our site for professional and enthusiast programmers.

up vote 11 down vote accepted

Either in a block or in a custom module that adds a block, add the following code:

if (user_is_logged_in() == TRUE) {
    global $user;

    print "Welcome " . $user->name;
}
else {
    print "Please log in.";
}

This is more useful if you want CURRENT user information. Possibly this as well for the user_is_logged_in function.

  • This worked fine. I found the solution by reading the first answer below this. However, i didn't know about the module for php content that is standard delivered with Drupal, so i had to search a bit further. Thanks for your reply. – Guan23 Jan 30 '12 at 15:50
  • No need for the call to user_is_logged_in() as that is just doing what you are already going by using the $user global variable. Just check $user->uid as per my answer below. – rooby Feb 2 '13 at 2:59

If you want to do this in a module (recommended as opposed to adding php code to a block, which would then not be in version control), then you can do this:

(in this case, all this code would go in a custom module named userwelcome.)

/**
 * @file
 * Adds a block that welcomes users when they log in.
 */

/**
 * Implements hook_theme().
 */
function userwelcome_theme($existing, $type, $theme, $path) {
  return array(
    'userwelcome_welcome_block' => array(
      'variables' => array('user' => NULL),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function userwelcome_block_info() {
  // This example comes from node.module.
  $blocks['welcome'] = array(
    'info' => t('User welcome'),
    'cache' => DRUPAL_CACHE_PER_USER,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function userwelcome_block_view($delta = '') {
  global $user;
  $block = array();

  switch ($delta) {
    case 'welcome':
      // Don't show for anonymous users.
      if ($user->uid) {
        $block['subject'] = '';
        $block['content'] = array(
          '#theme' => 'userwelcome_welcome_block',
          '#user' => $user,
        );
      }
      break;
  }
  return $block;
}

/**
 * Theme the user welcome block for a given user.
 */
function theme_userwelcome_welcome_block($variables) {
  $user = $variables['user'];
  $output = t('Welcome !username', array('!username' => theme('username', array('account' => $user))));
  return $output;
}

If you then wanted to override the themeing of this block in a theme you would do this (in your template.php file of your theme):

/**
 * Theme the userwelcome block.
 */
function THEMENAME_userwelcome_welcome_block(&$variables) {
  // Return the output of the block here.
}

Note that because this is a custom module you could also just update the theme funciton in the module directly.

If you don't want to use a custom module you can create a custom block with php code and add this:

global $user;
// Only for logged in users.
if ($user->uid) {
  print 'Welcome ' . theme('username', array('account' => $user));
}
  • This is a great answer, though I'm a little unclear about the naming and location of each of the functions. Here are my assumptions - can you advise if I am correct? Assuming theme name is theme_userwelcome, should function theme_userwelcome_welcome_block be placed in the theme not the userwelcome module. Presumably, userwelcome_theme function should actually be called userwelcome_theme_theme (yes 2 theme words as name of the theme replaces hook) & placed in the theme theme_userwelcome. functions userwelcome_block_view and userwelcome_block_info remain in userwelcome. – therobyouknow Sep 19 '13 at 13:29
  • I have updated the answer with this info: All this code goes an a module named userwelcome. If you wanted to override the themeing of the block from a theme you would use THEMENAME_userwelcome_welcome_block(&$variables) – rooby Sep 19 '13 at 22:28

Use views module. Create new view > show users > block display. Add contextual filter > provide default argument > user ID from logged in user. Configure fields (you can rewrite results) to include whatever text/tokens or user profile fields you wish. Save and add block to region.

Done with one module and no code.

-lunk_rat

  • I strongly believe this answer deservers more upvotes. This feels more 'the Drupal way'. If you can solve it with configuration, then do it that way — rather then using code. – Ideogram Aug 24 '14 at 6:10
  • @Ideogram that's my approach, too, which is why I submitted the answer even though many of the other solutions will work. Looks like it's getting some votes ... – Link Swanson Dec 8 '14 at 18:58

protected by Community Dec 5 '14 at 18:42

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.