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
Cracked it - I think!
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:
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?
Out of interest what are the
Out of interest what are the three [] variables after field_emp_no for?
Another newbie drupal user.
$emp_no =
$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
Check out get_custom_user_fields
Also check out the function get_custom_user_fields which is a better way to do this. For instance you could use
You can also get a list of custom fields using field_info_instances('user', 'user'), and there are other useful Field API functions.
Can't find get_custom_user_fields
When searching the API I get no results for get_custom_user_fields. Where does this function come from?
wrong title
Sorry, should have said check out field_get_items.
(get_custom_user_fields was my own function name..)
Only data from the {users}
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.
Thanks but now how do I add new users with added fields
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:
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
Best off starting a new
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.
I think this should
I think this should work:
Or like this:
Access to custom user fields : Drupal 7
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 :
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
Afaik, #default_value only
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
I get the notice -
Undefined index: value
Also request you to please follow this up at http://drupal.org/node/1281118