2

I am using the following to add a custom field to a user with a custom variable.

global $user;
  
$account = user_load($user->uid); 
 
$language = ($user->language) ? $user->language : 'und';
 
$edit = array(
  'field_favourite_football_team' => array(
    $language => array(
      0 => array(
        'value' => 'Manchester City',
      ),
    ),
  ),
);
   
user_save($account, $edit);

Problem: I can see the outputted value on module A but on module B, when I dump or print out that global $user, I can't see that field. Is there anyway to save that on a for global use?

  • 1
    I think $user is a array, use print_r($user); – user49951 Nov 9 '15 at 12:25
  • 1
    Use $user = user_load($user->uid) instead... – Clive Nov 9 '15 at 12:26
  • 1
    Dumb question: have you actually attached the field to the user entity? Admin >> Configuration >> People >> Account Settings >> Manage fields – Alfred Armstrong Nov 9 '15 at 12:29
  • 1
    @AlfredArmstrong Oh bloody hell - i thought creating it with PHP was all that was required...? – jagmitg Nov 9 '15 at 12:32
  • @AlfredArmstrong - that worked. Thanks for the help all :) – jagmitg Nov 9 '15 at 12:35
1

To add a field to the user entity, you need to visit the menu entry Administration > Configuration > People > Account Settings > Manage Fields. You can then define the field properties (including specifying whether it appears on the registration form or not) and customize how it is displayed on user pages.

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.