I needed a custom field that couldn't be provided by the core profile module (Select list populated from a SQL query). I was able to successfully add the field with proper options; however, I am unsure how to handle this new field once submitted.
From what I understand, I need to write a function that handles my SQL insert, and then call that function from a hook_form_alter submit button.
As of now, it is passing only the field name, not the value. And the field name is being serialized and stored in the 'data' field of the user table. I'm attempting to pass it to its own column.
Here is my code...
//takes value and inserts it to account field
function accountselect_submitaccount() {
db_query( "INSERT INTO {user} (account)
VALUES {account_name}" );
}
Then...
//call the above function using custom submit (I suspect this is the troubled area)
function accountselect_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'user-register')
$form['#submit'][] = 'accountselect_submitaccount';
}
To my knowledge, new fields are only added to the
data
column if there's no column matching the field name. You should be able to just create a column ofaccount
into the user table and it'll get populated with that data.As for the code you've posted, your submission function has a number of issues:
submit_function($form, &$form_state)
gives you access to the$form_state['values']
array.INSERT
will never work. You need anUPDATE
query.{account_name}
in the query. You need to actually use the value from$form_state['values']
.If I got you right, you are trying to alter the registration form of a user, so that beside inserting the "regualar" fields (username, password, etc...) the user will be requested to fill in additional fields. If I got it right, this is how I would proceed (as you said you are new to Drupal I tried to be as specific as I could).
Preliminary notes
[bread] => t('bread')
so that when using the form in other languages the choice will display "pain" or "bröd" or "pane", but the value returned (and possibly stored in the DB will be always be "bread".Design
hook_form_FORM_ID_alter()
where FORM_ID will be the ID of the form the user is going to use for registration. If there is more than one possible form to be used for registering, implementhook_form_alter()
instead, and create a switch/case structure in it.#element_validate
field to assign a validation callback, if you need to. You will have to append (as opposed to assign) the name of your submit callback function to the field#submit
of the button you use for submitting the entire form.$form
and$form_state
. The latter contains form values as selected by user. Your submit callback will be the one containing thedrupal_write_record()
instruction, to save relevant data in your custom table.Final notes
I discovered (thanks to another responder to this very same question) that Drupal does provide a mechanism for altering the user's table, indeed. I still do not like the design principle behind this, though:
An informative thread to read on this is however here. It might be finally worth mentioning that the underlying mechanism behind this feature is changing between D6 to D7.
HTH!