Cannot display a custom user field in php in Drupal 7 I added an array field with three possible values to the user. It is called field_user_school_category.  I am using Drupal 7.

I want to extract the field value for the current user and use it in a page in an if statement (in php text format). I just can't get the extract code to work. I can't even get the value to print - see code below. What am I doing wrong?!

The field is currently set up to accept multiple values. (I know I need to probably loop through to extract any other values later on). I have also tried using $school['und'][0]['safe_value'] instead of $school[0] amongst a myriad other things.

Any ideas or help would be very gratefully received.

Thanks Sheila

&lt;?php
global $user;
$account = user_load($user&gt;uid);
$school = field_get_items('user', $account, 'field_user_school_category');
$schoolcat = field_view_value('user', $account, 'field_user_school_category', $school[0]);
print $schoolcat;
Some problems, and their solutions:


Never use the PHP input filter.
When you are developing code, ensure that you have proper error reporting on. This would show you that you have a problem on the $account = line.


PHP 5.3: E_ALL | E_STRICT
PHP 5.4: E_ALL.

Your general problem is not in code per se, but in not understanding how to debug code.
If you have devel installed, you can dpm() values on a line per line basis, in an attempt to nail down the problem.
If you were to do that, you would quickly notice that $account does not contain the values you expect. $user&gt;uid is actually a mathematical operation, and will not return the users uid. What you want is $user-&gt;uid.
field_view_value() will, as the documentation suggests, return "a renderable array", it's not going to actually give you meaningful output, and since it's an array, you can't print it.


I'm guessing what you actually want is:

&lt;?php
global $user;
$account = user_load($user-&gt;uid);
$school_cats = field_get_items('user', $account, 'field_user_school_category');
foreach ($school_cats as $school_cat) {
  print $school_cat['safe_value'];
}