Referring to the original topic of the thread http://drupal.org/node/1031354, I have a select option in my user registration form.
Its options are coming from a taxonomy term.

Code similar to the above leaves me with an array :

$user_data = user_load($user->uid);
$dept_tid = $user_data->field_tuv_user_department['und'];

In devel I can see the tid is stored in field_tuv_user_department['und']['#default_value']['0'][, but
$dept_tid = $user_data->field_tuv_user_department['und']['#default_value']['0'];
gives "Undefined index: #dafault_value "

Pointers please.

Comments

patrickd’s picture

#default_value
#dafault_value

?

vivdrupal’s picture

That's already corrected as shown in the code. The error is with the corrected spelling '#default_value'.

patrickd’s picture

okay (this even was too simple to be true ^^)

did you already try to access '#value' instead of '#default_value'

What do you mean you know it by devel? did you activate to print the $page array or did you dsm($user_data)?

vivdrupal’s picture

Thanks for your response.

1. I tried '#value' instead of '#default_value' but got "Undefined index: #value"

2.I did dsm($form) and went to the field name and opened the drop-downs ...

3. dsm($user_data) gives "Undefined variable: user_data"

ibes’s picture

I guess your order is not correct

try: ['und']['0']['#default_value']

see this for more info: http://drupal.org/node/1031354#comment-5008970

vivdrupal’s picture

Thanks for your response.

vivdrupal’s picture

Undefined index: value

ullascs’s picture

@ vivdruPal, were you able to resolve this issue!

Myself stuck on a similar situation, need a contextual value from the user field, but gives the same error. For user itself, the php code is working but for additional fields added to user registration form, it gives this error!

Pls help if you were able tO resolve this!

Thanks.

vivdrupal’s picture

My final code is like this:

<?php
$user_data
= user_load($user->uid);
foreach (
$user_data->field_tuv_user_department['und']['0'] as $key_value => $option_value){
//drupal_set_message("user_data->field_tuv_user_department['und']['0'] : ".$option_value . " Key  :" . $key_value);
if($key_value ==='tid') {
            
$field_tuv_user_department_tid = $option_value ;
           
//drupal_set_message("field_tuv_user_department_tid : ".$field_tuv_user_department_tid);
                   
}
            }
$form['field_tuv_interview_user_dept']['und']['0']['value']['#default_value'] =  $field_tuv_user_department_tid ;
?>

Hope this helps.

ullascs’s picture

Wonderful. It works. Thank you!

nevets’s picture

That can be simplified using Field data extraction to

<?php
$user_data
= user_load($user->uid);
$form['field_tuv_interview_user_dept']['und']['0']['value']['#default_value'] =  field_extract_value('user', $user_data, 'field_tuv_user_departmen', 0, array('key' => 'tid')
?>