0

I have a error that keeps popping up through my website after I added tags to print custom user profile fields. The code i'm using is

<?php 
global $user;
$user_fields = user_load($user->uid);
$name = $user_fields->field_name;
$department = $user_fields->field_department;
?>
<?php

to pint the fields i'm using

<?php print $name['und'][0]['value'];?>
<?php print $department['und'][0]['value'];?>

i'm not sure if this is the correct way to do this or if there is a better way. I tried to remove [0] and the tag no longer works. I have also tried to use the following:

  <?php if (!empty($action_links)): ?>
  <?php print $name['und'][0]['value'];?>
  <?php endif ?>

This again did not display the custom field.

3
  • Please if you run dpm($user_fields->field_name) and post results for all fields which you want to print? As all fields don't have same structure as ['und'][0]['value'].
    – Xeeshan
    Commented Oct 3, 2014 at 12:30
  • Thanks for your response. Could you possibly elaborate a little further regarding this. I am fairly new to the drupal structure. Commented Oct 3, 2014 at 12:48
  • Install devel module. Than add following line to your code. dpm($user_fields->field_name); //where field_name is your user's field name. It will print the structure of your field and you will be able to see that where is your value stored?
    – Xeeshan
    Commented Oct 3, 2014 at 12:51

3 Answers 3

0

The proper way to print fields in Drupal 7 is to use field_view_field() to fetch a renderable array that can be passed to dupal_render() like so:

<?php
  global $user;
  $user = user_load($user->uid);
  print drupal_render(field_view_field('user', $user, 'field_name'));
  print drupal_render(field_view_field('user', $user, 'field_department'));
?> 

This is also true for $node objects FYI.

10
  • how do I print them out using your code? Commented Oct 3, 2014 at 13:13
  • I don't follow your question; printing is included in the code above.
    – Webdrips
    Commented Oct 3, 2014 at 13:17
  • I put the following code into my template and it doesn't print anything. I'm sorry if i'm missing something. Commented Oct 3, 2014 at 13:20
  • What template file are you using? Are your fields profile2 fields, or "regular" user fields? Have you cleared your caches?
    – Webdrips
    Commented Oct 3, 2014 at 13:23
  • I'm just using regular old user fields. Not using profile2 fields or anything like that. I created a template file for the user profile page i'm using so I named the file page--user.tpl.php Commented Oct 3, 2014 at 13:29
0

If you are wanting to render the field as drupal would in the "manage display" then use Webdrips answer. If you are simple wanting to render the value and have the entity API then you can do this:

<?php 
  global $user;
  $user = user_load($user->uid);
  $user_wrapper = entity_metadata_wrapper('user', $user);
  $name = $user_wrapper->field_name->value();
  $department = $user_wrapper->field_department->value();
?>
<?php
  print $name;
  print $department;
?>
0

These are the warnings because you are accessing any indexes that are not present in that field. Mainly occurres when these fields are not entered so these indexes are not created. You can use it with the isset to avoid these error/warnings

<?php print isset($name['und'][0]['value']) ? $name['und'][0]['value'] : "";?>
<?php print isset($department['und'][0]['value']) ? $department['und'][0]['value'] "";?>

OR

You can use the default drupal method to print field using field_view_field() function as mention in the earlier answer.

<?php
global $user;
$userFull = user_load($user->uid);
print render(field_view_field('user',$userFull,'field_name',$display=array() ));
//Where $display handles the label(hidden or to show), format etc
?>

Hope it will help!

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.