0

I'm working on a Drupal 7 portal. I added a custom field to the registration form and I would like to get hold of custom field value when the registration form is submitted and before it is stored in the database.

I'm trying to use the submission handler for the registration form and print the content of $form and $form_state, but those values never get printed as message. I installed the Devel module as well.

function user_register_form_submit($form, &$form_state) {
    drupal_set_message('<pre>'. print_r($form, 1) .'</pre>');
    drupal_set_message('<pre>'. print_r($form_state, 1) .'</pre>'); 
    drupal_set_message(t('An info message from user_register_form_submit'), 'error');
}

I appreciate any input on what the issue could be.

0

In hook_form_alter use foloowing code


function MODULENAME_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'user_register_form'){
    $form['#submit'][] = 'module_custom_form_submit';
   }
}

after that use


function module_custom_form_submit($form, &$form_state) {
    // Do whatever you want.
}

  • The question is Why isn't the content of $form and $form_state printed? – kiamlaluno Mar 6 '18 at 11:37
  • Are you talking about my solution? – Jeetendra Kumar Mar 6 '18 at 11:42
  • I am commenting on your answer. The OP is asking a different question. (I'm trying to use the submission handler for the registration form and print the content of $form and $form_state, but those values never get printed as message. I installed the Devel module as well. I appreciate any input on what the issue could be.) – kiamlaluno Mar 6 '18 at 11:48
  • The reason nothing is printed might be user_register_form_submit is not properly registered as a submission handler. Hence Jeetendra Kumar's answer. – Kirill Shabunov Mar 6 '18 at 12:04
  • @KirillShabunov This answer doesn't make clear the reason of the suggestion. If the OP doesn't understand what he is doing wrong, this answer doesn't explain what is wrong in the OP's code. – kiamlaluno Mar 6 '18 at 12:40
0

There are two issues in the shown code.

  1. Simply defining a function is not sufficient for Drupal to consider it a form submission handler, not even if the function name ends with _form_submit. To add a new form submission handler, the module you are developing needs to implement hook_form_alter() or hook_form_FORM_ID_alter(), and add a new form submission handler.

    function mymodule_form_user_register_form_alter(&$form, &$form_state) {
      $form['#submit'][] = 'mymodule_user_register_submit';
    }
    
  2. To debug a form submission handler you should use watchdog(). Once the Database logging module is installed, on admin/reports/dblog you can read all the messages added with watchdog().

protected by kiamlaluno Mar 6 '18 at 12:44

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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