Community

Programatically update/insert values in custom user 'profile' fields at/after account create.

I am building a module that will automatically add values to the extra field(s) attached to a user at/after account create. I am using shibboleth to authenticate, and can get the values form the $SERVR variable. The problem is (I THINK) that the $SERVER vars aren't populated at first, so I have to let shibboleth create the user, then do something in user_insert or user_update. I'm working in D7
here's what Ive done:

-Create a field in admin/config/people/accounts/fields called (machine name) field_profile_empl_id
-made a module called myMod
-made a call to hook_user_update:
-I can get the shibboleth vars, but when I try to call a user_save, I get the white-screen-of-death

function myMod_user_update(&$edit, $account, $category) {
drupal_set_message('this is your account id '. $account->uid); //this works for the account in question
drupal_set_message("shibboleth vars: ".($_SERVER['employeeID'] ) ); //this works as well
$account = user_load($GLOABALS['user']->uid);

$edit = array(
'field_profile_empl_id' => array(
'und' => array(
0 => array(
'value' => ($_SERVER['employeeID'] )
)
)
)
);

user_save($account, $edit)
}

If anyone can help me or point me to a working example, I would be forever grateful!

Dan

Comments

First off, making the code a

First off, making the code a little easier to read:

<?php
function myMod_user_update(&$edit, $account, $category) {
 
drupal_set_message('this is your account id '. $account->uid); //this works for the account in question
 
drupal_set_message("shibboleth vars: ".($_SERVER['employeeID'] ) ); //this works as well
 
$account = user_load($GLOABALS['user']->uid);

 
$edit = array(
   
'field_profile_empl_id' => array(
     
'und' => array(
       
0 => array(
         
'value' => ($_SERVER['employeeID'])
        )
      )
    )
  );

 
user_save($account, $edit)
}
?>

--
Damien McKenna | Mediacurrent

A small thing - this part is

A small thing - this part is incorrect:

<?php
         
'value' => ($_SERVER['employeeID'])
?>

The parenthesis are not necessary, it should be just:

<?php
         
'value' => $_SERVER['employeeID'],
?>

A bigger issue is that you're overwriting the $account object, you should just use the $account object that's passed in.

Also, using the $GLOBALS['user'] value rather than the $account object makes it so that an admin can never update someone else's account.

You might want to do some checks to see if the empl_id value needs to be updated, otherwise this will update the value every single time any account updates are made.

You should also review a blog post from Dominique De Cooman that explains the correct way of updating individual fields on an entity in D7: http://dominiquedecooman.com/blog/drupal-7-tip-update-and-insert-only-sp...

--
Damien McKenna | Mediacurrent

Thanks!

I am going to wrap my head around this next week, I'll report back on how far I get.
(or wrap this around my head!)