0

I've added few more fields in "Administration » Configuration » People » Account settings » Manage fields". See the attached screenshots for more details.
Now I want to display user's First name & Last name in page.tpl file. I know that name can be diplayed like this "$user->name". But I want to know how other filed values can be displayed. I want to display the fileds First name, Last name, Phone, Address

enter image description here

3

If you want to display first name and last name of the logged in user, then you can follow the below code snippets.

global $user;

$account = user_load($user->uid);

print "First name:" . $account->field_f_name[LANGUAGE_NONE][0]['value'];
print "Last name:" . $account->field_l_name[LANGUAGE_NONE][0]['value'];

The global $user object does not load the fields which are attached to user profile. So, We need to use function user_load() in code.

If you want to display First Name and Last Name of the user profile, then you need get uid of the user from the arguments.

$uid = arg(1);

$account = user_load($uid)
0

A better way is using the Field API.

I'm not sure which variables are you handling on the page.tpl.php

As harsal says, you can use the global variable $user, but to access each field of the current user, you could do it like this (for example for phone would be):

echo render(field_view_field('user', $user, 'field_phone', array('label' => 'hidden')));

Additional tip: to protect each field against XSS attacks, you cloud use the Drupal function filter_xss()

0

The following can be implemented to get the user details

global $user;
$user_load = user_load($user->uid);
$user_load->field_f_name[LANGUAGE_NONE][0]['value'] ;
$user_load->field_l_name[LANGUAGE_NONE][0]['value'] ;
$user_load->field_phone[LANGUAGE_NONE][0]['value'] ;
$user_load->field_address[LANGUAGE_NONE][0]['value']

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.