0

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

<?php
global $user;
$account = user_load($user>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;
1
  • Hello Sheila, welcome to StackExchange :) I posted an answer below, I hope it's length does not look to frightening. Getting into coding is not always easy, which is why it's often recommended to use already existing modules instead of writing your own code. Is there any particular reason you are doing this in code? – Letharion Apr 15 '12 at 9:18
1

Some problems, and their solutions:

  1. Never use the PHP input filter.

  2. 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.

    1. PHP 5.3: E_ALL | E_STRICT
    2. PHP 5.4: E_ALL.
  3. 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>uid is actually a mathematical operation, and will not return the users uid. What you want is $user->uid.

  4. 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:

<?php
global $user;
$account = user_load($user->uid);
$school_cats = field_get_items('user', $account, 'field_user_school_category');
foreach ($school_cats as $school_cat) {
  print $school_cat['safe_value'];
}
2
  • Thanks, Letharion, for such a quick reply. I am still feeling my way with Drupal php and trying to run before I can walk perhaps. I'll install devel and get up to speed with using that. I'll also have a go with the code you suggested - that is very helpful - thanks again. – Sheila Apr 15 '12 at 13:48
  • Couldn't fit this all in the first post... FYI, I have a front page which is built with php code which displays blocks of notices depending on a user's role. What has happened now is that the senior staff want users to be able to filter notices depending on whether they are in Kindergarten, Primary or Secondary. I've added the field and was going to test for the field setting on the current user to determine which block to show. However I couldn't access the field in order to test it. – Sheila Apr 15 '12 at 14:00

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.