1

This is for Drupal 7. I am trying to write my first real custom module but I'm running into some difficulty and I'm just not sure how to look for my answers.

I've created a new entity for my module (with Entity API) with a corresponding table in the database. I need to add records to this table upon new user registration. I've used Form API to add a new field to the user registration form but I'm not quite sure the best way to do the next step. When the registrant submits the form, I would like all the regular Drupal registration stuff to happen, but I would like to intercept this one field and have its data go to the table I've created for my entity.

Here's what I have in the .module file of my custom module (the part concerning the registration form):

/**
* Implement hook_form_user_register_form_alter()
*/
function my_module_form_user_register_form_alter(&$form, &$form_state, $form_id){
    //add my module textbox to the registration form
    $form['my_module_field'] = array(
        '#type' => 'textfield',
        '#title' => t('My Module field'),
        '#required' => TRUE,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'New Submit Button',
        '#submit' => array('my_module_form_submit'),
    );
    unset($form['actions']);
}//end my_module_form_user_register_form_alter

/* Custom Submit handler */
function my_module_form_submit($form, $form_state){
    db_insert('my_entity_table')->fields(array(
        'my_entity_item' => $form_state['values']['my_module_field'],
    ))->execute();
    drupal_set_message('Your information has been saved.');
}//end my_module_form_submit

This is working fine to get my special field in my special table. But the user is not created. I suppose I could do it manually (like enter the other information into the appropriate users table and everything) but obviously I would rather not do that manually.

I feel that I must be missing something obvious here but I'm not having any luck searching for it. I am self-taught in the little I know of programming and Drupal.

Is there a way for me to just jump in to the regular submission handler and say, "hey, put my field here instead!"?

Thank you for your time.

1

1 Answer 1

1

Wow. I puzzled on this all day and 30 minutes after asking this question, of course I find the answer.

Since the $form['submit'] part is an array, you can have more than one handler there! I changed the lines

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'New Submit Button',
    '#submit' => array('my_module_form_submit'),
);
unset($form['actions']);

to simply be

$form['#submit'][] = 'my_module_form_submit';

and that's it! Now it does the regular registration submission handler and also mine.

Thanks!

2
  • Just a side note, since it is an entity, you can do entity_create('my_entity'), add your data, then call entity_save($entity) instead of db_insert. See the answers here: drupal.stackexchange.com/q/7271/57 - this will allow you to potentially act on this event with other modules, like Rules, later if you need to do so.
    – Kevin
    Commented Nov 18, 2016 at 21:19
  • This is also how you should save data to an entity table. If You went all the rouble of creating an entity type, use the entity API for persistence, don't directly access the database. Commented Nov 18, 2016 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.