0

In my html.tpl.php , I was printing this:

    print $user->name;

which displayed username.

But now I need to print Custom field "First Name" instead of username.

So I tried this:

   print $user->field_first_name[und][0]['value'];

which is not printing anything although the field "First Name" has some value.

Where am I going wrong ? Am I missing some obvious thing ?

Thank you .

1 Answer 1

1

If you are using the global variable $user, it doesn't always contain a full initialized object. In fact, during the bootstrap, $user is initialized with the data contained in the "users" table, from the following code.

  if ($is_https) {
    $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.ssid = :ssid", array(':ssid' => $sid))->fetchObject();
    if (!$user) {
      if (isset($_COOKIE[$insecure_session_name])) {
        $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid AND s.uid = 0", array(
          ':sid' => $_COOKIE[$insecure_session_name],
        ))
        ->fetchObject();
      }
    }
  }
  else {
    $user = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", array(':sid' => $sid))->fetchObject();
  }

  // We found the client's session record and they are an authenticated,
  // active user.
  if ($user && $user->uid > 0 && $user->status == 1) {
    // This is done to unserialize the data member of $user.
    $user->data = unserialize($user->data);

    // Add roles element to $user.
    $user->roles = array();
    $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
    $user->roles += db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = :uid", array(':uid' => $user->uid))->fetchAllKeyed(0, 1);
  }

If you want to be sure to get a fully loaded user object for the currently logged-in user, you should use the following code.

$account = user_uid_optional_load();

After that, you can get the value of any user field.

$fullname = field_get_items('user', $account, 'field_first_name');

You can also get the rendered value of the field using the following code.

$rendered_value = field_view_field('user', $account, 'field_first_name');

The other reason for which $user->field_first_name['und'][0]['value'] could not contain any value is that the language-neutral value of the field is not be set. ('und' is the value of the constant LANGUAGE_NONE, or LANGUAGE_NOT_SPECIFIED in Drupal 8.) If you use field_get_items(), or field_view_field(), you are sure to get the correct value.

As side note, it is preferable to keep the logic in the template.php file, in functions that are called from the template files.

8
  • Thank you @Kiamlaluno for the answer. I tried doing this: $fullname = field_get_items('user', $user->uid, 'field_first_name'); in html.tpl but did not get any value when I printed $fullname or $fullname['value']. Nov 19, 2012 at 7:52
  • 1
    You need to pass the user object, not the user ID.
    – apaderno
    Nov 19, 2012 at 8:02
  • I tried without uid too, using simply $user but it also did not print anything. Nov 19, 2012 at 8:08
  • And when I print $user->field_first_name['und'][0]['value'] in user-profile.tpl.php it prints the value but it is not printing when used in html.tpl.php Nov 19, 2012 at 8:10
  • How to you initialize the $user variable?
    – apaderno
    Nov 19, 2012 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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