Drupal 8 Quick Tips: Adding an extra form submission handler
09/02/2016
  • Jens Steppe

  • Team Lead

Have you ever wanted to do something extra after a form submit on a core form when no hooks are available?

function mymodule_form_user_login_form_alter(&$form, FormStateInterface $form_state)

{

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

}

function 'mymodule_extra_submit_handler'($form, FormStateInterface $form_state)

{

 // Run code here.

 // This will run after the standard submitForm for the user login form. 

}

That's all you need to have a custom function executed after the core code. You can use this little trick with submission or validation handlers, both will work. 

Hope this helps, and saves you some time.

 

Verwante Artikels