I understand how to add stuff like checkboxes and fieldsets to existing forms (e.g. the form displayed on the node edit page) using _form_alter. But what I don't understand is how to collect the input and store it in a database table of my choosing (ideally with db_insert). Let's say I've added a checkbox to the fieldsets at the bottom of article_node_form (the form which appears when editing content type article):

$form["Email Notifications"] = array(
    '#type' => 'fieldset',
    '#title' => 'Email Notifications',
    '#collapsible' => 'true',
    '#group' => 'additional_settings',
    '#required' => FALSE,
    'email_notify' => array (
        '#type' => 'checkbox',
        '#title' => 'Notify users of changes to this page',
        '#default_value' => 0,
        '#required' => FALSE,

How do I collect this data an store it in a database?


Exactly like you alter a form to add custom fields, you alter it to add custom validation and submit handlers.

It should be:

$form['#validate][] = 'custom_validate';
$form['#submit'][] = 'custom_submit';

use those function to control the additional user input and store them later.

|improve this answer|||||
  • how would I name my submission handler in my module, supposing my module is called "my_module," the form has the id of "the_form" and the custom submit field is "custom_submit"? It's information like this I can't find anywhere. it seems nothing is ever explicit in the drupal documentation... – CHRIS Jul 17 '14 at 19:36
  • Well, basically, choose the name you want, as long as is not in use... that means it's considered best practice for your module to use name like mymodule_anynameilike. However, i wouldn't call a field "custom_submit", in your case i would call it "email_notification", because it's another best practice to use names that are... self-describing. – Astrayus Jul 17 '14 at 19:49
  • Think about it, you try to alter a form, and you find a field called "custom_submit". And you'll be like: what the hell is this? – Astrayus Jul 17 '14 at 19:50
  • hmm, well the problem I am running into now is that I get an infinite recursion when I try to access $form["Email Notifications"]["email_notify"] in the submission handler. Any ideas? I just want to be able to see if the checkbox is checked within my module. – CHRIS Jul 17 '14 at 20:14
  • I'm an idiot. Just needed to do: $form["Email Notifications"]["email_notify"]['#value'] – CHRIS Jul 17 '14 at 20:30

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.