0

I created a new user. When they logs in, they should to see their user name in the header region.

I can create a block for header but I am not sure how to create a block that displays the username of the logged in user.

1
  • Create a custom module and put your project related custom functionality in there. In this case you create a custom block in code. Or you can override the region in question and place the code in there, ie more of a theming solution. Jun 7 '15 at 14:03
3

Using php filters is not recommended.

Instead create a User view for it.

Steps to creating a user view:

  1. Go to Admin -> Structure -> Views
  2. Click on "Add new View"
  3. Now simply create a User view.

enter image description here

  1. Click on Continue and edit button.
  2. Add a contextual filter for uid.

enter image description here

  1. Click on apply button. And the filter value as "User Id from logged in user".

enter image description here

  1. Click on Apply button.
  2. In the field, add the name of the user as the field to display. If you want to show more things just add more fields to it.

enter image description here

  1. Save the view and place the block in the header region.
2
  • Why run another SQL query to fetch the user object when it is already available? Seems wasteful. Jun 7 '15 at 13:59
  • @Drupol: If you feel you got the answer, can you make this as the answer, so that if anyone else have the same question, he can quickly get the solution. Jun 7 '15 at 18:31
2

I agree with @Nitesh, using php filters is not recommended, here is another aproach by creating a custom block by code named 'Display user name', it will work only for authenticated users, place block in propper region, make a custom module named 'hello_user_block', and place this code inside hello_user_block.module

<?php
/**
*implements hook_block_info
*/
function hello_user_block_block_info() {
  // This example comes from node.module.
  $blocks['user_name'] = array(
    'info' => t('Display user name'),
    //'cache' => DRUPAL_NO_CACHE
  );
  return $blocks;
}

/**
*implements hook_block_view
*/
  function hello_user_block_block_view($delta = '') {
  // This example is adapted from node.module.
  $block = array();

  switch ($delta) {

    case 'user_name':
      $block['subject'] = t('');
      //$block['type'] = 'markup';
      $block['content'] = user_name_function();
      break;
  }
  return $block;
}

/**
*implements function block content
*/
function user_name_function() {
  global $user;
  $output = '';
  if($user->uid!=0){
    $output = 'Hello '.$user->name;
  }

  return $output;
}

EDIT: this code makes 2 blocks, first block just output same as before: 'Hello user-name', second block output: 'Good monrnig user-name', or: 'Good afternoon user-name', or: 'Good evening user-name' depending on time of day, you can use either block.

    <?php
/**
*implements hook_block_info
*/
function hello_user_block_block_info() {
  // This example comes from node.module.
  $blocks['user_name'] = array(
    'info' => t('Display user name'),
    //'cache' => DRUPAL_NO_CACHE
  );

  $blocks['user_name_greetings'] = array(
    'info' => t('Display user name & greetings'),
    //'cache' => DRUPAL_NO_CACHE
  );

  return $blocks;
}

/**
*implements hook_block_view
*/
  function hello_user_block_block_view($delta = '') {
  // This example is adapted from node.module.
  $block = array();

  switch ($delta) {

    case 'user_name':
      $block['subject'] = t('');
      //$block['type'] = 'markup';
      $block['content'] = user_name_function();
      break;

    case 'user_name_greetings':
      $block['subject'] = t('');
      //$block['type'] = 'markup';
      $block['content'] = user_name_greetings_function();
      break;
  }

  return $block;
}

/**
*implements function block content
*/

function user_name_function() {
  global $user;
  $output = '';
  if($user->uid!=0){
    $output = t('Hello ').$user->name;
    $output = "<div class='float-right'>$output</div>";
  }
  return $output;
}


function user_name_greetings_function() {
  global $user;
  $output = '';
  $day_term = '';
  if($user->uid!=0){

    $hour = date('H');

    if ($hour>11 && $hour<19) {
      $day_term = t('Good afternoon');
    }

    if ($hour>18 && $hour<=24) {
      $day_term = t('Good evening');
    }

    if ($hour>=0 && $hour<12) {
      $day_term = t('Good monrning)';
    }

    $output = $day_term.' '.$user->name;
    $output = "<div class='float-right'>$output</div>";
  } 
  return $output;
}

it adds a class 'float-right', add this code in your custom css file.

.float-right {
 text-align: right;
 color:rgba(121, 121, 121, 1);
 font-size: 12px;
 font-style: italic;
 position: relative;
 top:-10px;
 }

placed in 'top bar' region it will otuput something like this, sorry, my img example is in spanish: Hola root, buenas tardes! --> Hello root, good afternon!

enter image description here

1

The user object contains the current user properties. The following properties of the user object is available.

enter image description here

So in order to print the user name,enable the PHP Filter module, Add a block and then you need to select PHP as the text format for that block, as this picture shows

enter image description here

and then in the block body write

global $user;
print $user->name;

Now assign the block at any where you want

3
  • 1
    We should not user php filters in the site. We can achieve the same thing by creating a simple view for logged in users. Jun 7 '15 at 5:31
  • There is no problem with using this module. There are some cases that you will have no way out but using this module.
    – M a m a D
    Jun 7 '15 at 6:11
  • For corner cases we can use php Filters. But ideally we should avoid using it. Jun 7 '15 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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