1

I have a system for storing data (e.g. accessibility settings like colour contrast) for anonymous and authenticated users. Authenticated users have the data in a dedicated table.

For anonymous users I'm using CTools object cache so if the person keeps the session he won't lose his accessibility settings. (I might use this for storing searches as well.)

I could use Session API but there is a potential issue with European cookie law - yes, this is about site usability which theoretically trumps that law but it's a very public site so the client doesn't want any trouble, valid or not.

Okay, so here's the point: If a person registers I need to go from CTools to my custom storage. The actual process of doing that is simple enough but when a person registers, their session ID changes, so once they are logged in (e.g. when hook_user_register() gets called) the old session ID is dead and gone.

Any ideas how I can keep the old session ID, or transfer the data from not-logged-in to logged-in? (Putting the data in form_state in the user_register_form goes some of the distance, but not all of it.)

  • 1
    In fact it turned out to be simple (after spending a long time trying to be clever). In hook_form_user_register_form_alter() add a value field to the form... $form['mydata'] = array( '#type' => 'value', '#value' => $mydata, // in any form you like ); Then in hook_user_insert() that data is in the $edit array, and already added to the $account object. Make sure you unset it from the $edit array otherwise it'll get put into the 'data' field. Anyway by this point the $account has a uid so it's possible to save the data. – Adaddinsane Apr 17 '12 at 15:05
  • 1
    In the future, instead of leaving a comment, you should answer your own question. (Drupal Answers encourages users to answer their own questions.) – Patrick Kenny Apr 29 '12 at 1:57
  • There was a particular reason I didn't - though offhand I can't remember what it was. – Adaddinsane Apr 30 '12 at 14:43
2

In fact it turned out to be simple (after spending a long time trying to be clever).

In hook_form_user_register_form_alter() add a value field to the form...

$form['mydata'] = array( 
  '#type' => 'value', 
  '#value' => $mydata, 
  // in any form you like 
); 

Then in hook_user_insert() that data is in the $edit array, and already added to the $account object. Make sure you unset it from the $edit array otherwise it'll get put into the 'data' field. Anyway by this point the $account has a uid so it's possible to save the data.

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.