Warning! Newbie Question:

I am trying to get to grips with Drupal to provide a solid and secure framework for my (currently) hand build intranet (PHP/Mysql).

Having loaded and installed Drupal 7 on the Aquia drupal stack I am stuck at pretty much first base.

My first (PHP enabled) content page attempt is to simply access the profile of the current user.

The following works perfectly:

global $user;
print_r($user);

However, I have added one custom field to the user account with field name 'field_emp_no' which is intended to provide a key link to an external database which has pretty full on employee data.

The above code does not show the new field in the user object. Using $user->field_emp_no directly doesn't do it ether.

Anyone switch on the lights for me?

Thanks

Comments

crislewis’s picture

Hmm, it's all a bit cumbersome and counter intuitive but it seems that fields added to the user are not available after the user is logged on until you force a reload using user_load().

I ended up with the following:


$user_data = user_load($user->uid);

$emp_no = $user_data->field_emp_no['und']['0']['value'];

Which does seem hard work for such a simple activity. Anyone want to give me a simple reason the value is buried this way?

I guess I have a very steep learning curve ahead of me to get to grips with Drupal API. Is there a way to force Drupal to load added field values into the user object at logon so that they are available by simply referring to them as $user->emp_no for example?

shags_j’s picture

Out of interest what are the three [] variables after field_emp_no for?

Another newbie drupal user.

ibes’s picture

$emp_no = $user_data->field_emp_no['und']['0']['value']

['und'] is for the language - in this case this might be "undefined" -> multi-lingual pages
['0'] is for the first item (PHP counts 0, 1, 2, 3...) -> some fields allow multiple values
['value’] is for the value of the field - there are some more information on this array-level - for example default value and stuff

Bagz’s picture

Also check out the function get_custom_user_fields which is a better way to do this. For instance you could use

$fieldinfo = field_get_items('user', $account, 'field_emp_no');
if ($fieldinfo) {
  $fieldvalue = check_plain($fieldinfo[0]['value']);
}
else {
  $fieldvalue= '';
}

You can also get a list of custom fields using field_info_instances('user', 'user'), and there are other useful Field API functions.

spet’s picture

When searching the API I get no results for get_custom_user_fields. Where does this function come from?

Bagz’s picture

Sorry, should have said check out field_get_items.

(get_custom_user_fields was my own function name..)

jaypan’s picture

Only data from the {users} table is added to the $user object at load, to keep things streamlined. To load your external data, you will need to call user_load() with a $uid in order to generate a fully loaded user object.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

crislewis’s picture

Thanks for your help.

I've now got access to the added field and can use it to get data from the external database.

Now I also wish to be able to create users using data from the external database.

I have managed to do the basics with user_save() but I can't work out how to include a value for the added field in the creation process.

In it's simpllest form I have:


$new_user = array(  name=>joebloggs,
                            password=>easy,
                            mail=>joe@somewhere.com,
                            status=>1,
                            init=>joe@somewhere.com,
                            field_emp_no=>WMS0001);

$new_user= user_save(null,$new_user);

The creates the user but does not populate the added field. The object returned does have a field_emp_no attribute with a value of 'WMS0001' but this is not the same.

Thanks

jaypan’s picture

Best off starting a new thread on this since it has nothing to do with the original topic.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

mariusilie’s picture

I think this should work:

$new_user = array(  name=>joebloggs,
                            password=>easy,
                            mail=>joe@somewhere.com,
                            status=>1,
                            init=>joe@somewhere.com,
                            field_emp_no=>array("und" => array(0 => array("value" => WMS0001))),

);

Or like this:

$new_user["field_emp_no"]["und"][0] = array("value" => $user_profile["id"]);
vivdrupal’s picture

Referring to the original topic of the thread, 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.

Moved my request to http://drupal.org/node/1281118

duckzland’s picture

Afaik, #default_value only available in forms, when you are accessing the saved data from user_load, you should use [0]['value'] instead.

Also you should use $user->language to replace the 'und' otherwise if you have multilanguage site it will breaks when you try to use 'und' while in 'en' page

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

vivdrupal’s picture

Undefined index: value

Also request you to please follow this up at http://drupal.org/node/1281118