1

I've altered users table and added two new fields directly using MySQL workbench

  1. first_name
  2. last_name

If we add new fields by config > people > site information > manage fields, it does not allow adding drop-down list. So, directly added using MySQL workbench.

Now I was able to alter user_register_form and added above two fields with validation. At the time of creating new user, the values of first_name and last_name gets stored in users table.

Now, how to get those two fields when user logs in?

i've used below code to print user object

global $user;
$user_full = user_load($user->uid);
print_r($user_full);

Any help would be appreciated, thanks !!

1
  • This is not the way to do it ! Use the entity module to add field to the user object just like you add field to a content type. When you create your field tick the "Display on user registration form." option.
    – pbonnefoi
    Jun 26, 2014 at 14:31

1 Answer 1

1

You can implement the hook_user_load() for this purpose

I have altered the user table with field "fname" , Now you can load this in user object with the help of hook_user_load() func.

function testing_custom_user_load($users) {
  foreach ($users as $record) {
     foreach ($users as $record) {

        $result = db_query("SELECT fname FROM {users} WHERE uid IN ($record->uid)")->fetchAssoc(); 

        $users[$record->uid]->extra =$result['fname'];
     }
  }

}

Just put this code snippet in any of your custom module file.

2
  • I want those fields in $user object, how it can be done and where should the above code placed ?
    – harnish
    Jun 27, 2014 at 4:59
  • Answer updated, Please check. Jun 27, 2014 at 13:47

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.