Get custom field value in Drupal 7 template?

I am building my first website with drupal. And I created a custom field: Full name. Now I want to get the value of this fild in my template to say "Hello,% username%". How to do it?

+6
source share
4 answers

Depending on your setup / field name, something like this in template.php (preprocess function for the template file):

 function mytheme_preprocess_page() { global $user; $user = user_load($user->uid); // Make sure the user object is fully loaded $vars['full_name'] = $user->field_full_name[LANGUAGE_NONE][0]['value']; } 

Then something like this in page.tpl.php :

 if (isset($full_name) && !empty($full_name)) : echo 'Hello ' . $full_name; endif; 

Please note that LANGUAGE_NONE may need to be changed if you use a multilingual site.

+11
source

The answer to Clive is correct, except that you must use field_get_items to get the values ​​for the field. It will process the language for you. You must also sanitize the value.

 function THEME_preprocess_page() { global $user; $user = user_load($user->uid); // Make sure the user object is fully loaded $full_names = field_get_items('user', $user, 'field_full_name'); if ($full_names) { $vars['full_name'] = check_plain($full_names[0]['value']); } } 

If your site uses the Entity API module , you can also use an object metadata wrapper similar to this

 function THEME_preprocess_page() { global $user; $user = user_load($user->uid); // Make sure the user object is fully loaded $wrapper = entity_metadata_wrapper('user', $user); $vars['full_name'] = $wrapper->field_full_name->get(0)->value(array('sanitize' => TRUE)); } 

See also Writing Robust Code That Uses Fields in Drupal 7

+19
source

I know this question was asked a long time ago, but I wanted to post an alternative. It looks like you want to change the field in the $ variables array, which $variables['name'] matches what you have in the custom field that I called field_real_name . If you use the preprocess function, there is no need to retract the global $user object. You have access to the $variables array, so you can get information about the user with this - it will load the information related to uid ( see Template_preprocess_username ):

 function mythemename_preprocess_username(&$variables) { $account = user_load($variables['account']->uid); ...more code will go here in a moment } 

If you are dpm($account) (or kpr($account) , if you are not using devel), you will see that you have access to all user information without using the global $user object.

Then you can change the output of $variables['name'] as your field_real_name as follows:

 function mythemename_preprocess_username(&$variables) { // Load user information with user fields $account = user_load($variables['account']->uid); // See if user has real_name set, if so use that as the name instead $real_name = $account->field_real_name[LANGUAGE_NONE][0]['safe_value']; if (isset($real_name)) { $variables['name'] = $real_name; } } 
0
source

We can add the code below in any template file.

 <?php global $user; $user = user_load($user->uid); print $user->name; ?> 
0
source

Source: https://habr.com/ru/post/901445/


All Articles