0

What would be the proper hook to redirect after the user registration form is submitted and is in valid state?

I'm trying to automatically create a certain content-type node when a user registers and I want to redirect the user to that node's edit form after the user creation form is submitted and validated.

I was thinking of doing this in a mymodule_user_form_submit hook, but not sure if a) this hook exists and b) the validation in place for user registration will still work as intended.

Can anyone share some pointers?

EDIT:

Would the mymodule_user_insert hook run after the validation and submission of the form? Can I redirect from there? AFAIK, I won't have a form_state variable to act on, right?

EDIT2:

While researching for a solution, I noticed I have another (probably bigger) problem: the content-type node I need to create must hold a reference to the user's ID, so I'm getting in a chicken-or-egg situation.

I actually need to create said node after the user is created (otherwise I'll have no user_id), but I need to redirect to the node's edit form after user creation, which means I can't redirect with $form_state.

I think I got everything right, but I would love to be wrong. :D

Can anyone help?

0

If you have created a custom user registration form my_module_user_register_form, then you can define one or more submit functions for it

function my_module_user_register_form($form, &$form_state) {
    $form['#submit'][] = 'my_module_user_register_form_submit';
    // and now add a second submit function if you like
    $form['#submit'][] = 'my_module_user_register_form_another_submit';
}

and then place the appropriate redirect at the end of the last submit function.

function my_module_user_register_form_another_submit($form, &$form_state) {
   // do other stuff here 
   $form_state->setRedirect('node/123/edit');
}
  • 2
    drupal_goto does not exist in D8 – Joum Aug 31 '17 at 15:17
  • Never send a response yourself, especially not in the middle of a form submission; that's a sure fire way to break something. Use $form_state->setRedirect(). Also small thing, the method names in your examples don't match – Clive Aug 31 '17 at 15:25
  • @Monkeybrain thanks for the updated replies. I just noted a bigger issue, though, and posted it in my question above. Any more ideas? – Joum Aug 31 '17 at 15:33
  • Yes - define multiple submit functions, the first one does the user creation, the second one does the node creation and the redirect to the node form.You could pass any necessary data between the submit functions in a $_SESSION variable. – Monkeybrain Aug 31 '17 at 15:37
  • Sounds legit... Thanks! Can you elaborate or share an example of "multiple submit functions"? – Joum Aug 31 '17 at 15:41

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.