1

I recognize there is a similar question here:

drupal.stackexchange.com/questions/38124/hook-user-insert-get-form-values

but I've got Eclipse and XDebugger setup so I can see the variables and I am still stuck.

I provide the variables below, please note field_foo is a field the system has taken care of properly without my interfering, after the user submits it, field_bar is a field I want to change the submitted value of, it has the value 303031 when the system handles it from form submission, where the variables show "value 300501 " is my attempt to edit that value - clearly I'm editing something wrong)

$edit   Array [37]  
[0...30]    
    uid 63  
    field_foo   Array [1]   
        und Array [1]   
            0   Array [1]   
                value   asdasd  
                    length  6   
                    0   61  
                    1   73  
                    2   64  
                    3   61  
                    4   73  
                    5   64  
    field_bar   Array [2]   
        und Array [1]   
            0   Array [1]   
                value   303031  
        value   300501  

Here is my code to try and set it...

function MY_MODULE_user_insert(&$edit, $account, $category) {

    $newBarValue = 300501;
    $edit[field_bar][value] = $newBarValue;

}

PLEASE NOTE:

I don't know if this is the case for all field_data_field Core fields but I had to use the following to get it to work:

$edit[field_bar][und][0][value] = $newBarValue;

This might be unique as I am working on someone else's work atm.

3

hook_user_insert() is invoked after the user object and all it's fields have already been committed to the database; that's why your code isn't taking effect at the moment.

There are a couple of ways you could go:

  1. Use exactly the same logic in hook_user_presave(), which gets called before the data is committed to the database.
  2. Implement hook_form_alter() for the registration form, add a custom validation function, and manipulate the field values in there.
|improve this answer|||||
  • Thanks, I used hook_user_presave() as for some reason hook_form_alter() was having no effect on the user registration form. Thanks a lot – ja_him Oct 17 '12 at 8:19
  • Actually, that hasn't worked - hook_user_presave() certainly makes more sense, but I think it might be my array editing that is causing trouble atm. I will read up to see if for a field_data_field Core field I should be editing it in a different way. Any thoughts on that? – ja_him Oct 17 '12 at 8:39
  • PLEASE NOTE MY EXTRA BIT when following this answer - the problem was I needed to use this: $edit[field_bar][und][0][value] = $newBarValue; to edit the correct part of the array – ja_him Oct 17 '12 at 8:52

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.