I have a form which I want to modify some of it's data after submission.

I'm using drupal 8.3 and I tried to used hook_form_alter and registered my functions to $form['#submit'][] and $form['actions']['submit']['#submit'][] but none of them working after form submission.

Any help? Thanks.

  • Is the code being hit? Have you debugged it? – Kevin Sep 18 '17 at 16:47
  • nah it doesn't hit. – Lost Koder Sep 18 '17 at 16:50
  • Need to see all the code – Kevin Sep 18 '17 at 16:50
  • you need your code after the form submit? – Adrian Cid Almaguer Sep 18 '17 at 16:53

I added the following snippet in hook_form_alter:

array_unshift($form['#submit'], 'callback');
foreach ($form['actions'] as $action => $values) {
    if (isset($form['actions'][$action]['#submit']) && is_array($form['actions'][$action]['#submit'])) {
        if (!in_array('callback',$form['actions'][$action]['#submit'])) {
            array_unshift($form['actions'][$action]['#submit'], 'callback');

This registers to all actions.

  • Note that if you are not limiting by a $form_id, this code will happen for every form in the site (front end and backend) upon submission. – Kevin Sep 18 '17 at 17:04
  • That, also in 8.4.x, this will no longer be necessary as the dropbutton save pattern has been removed from node forms, it now uses a published checkbox + a single save button. – Berdir Sep 18 '17 at 20:03

you did n't mention you are tring that custom submit handler to core forms or your custom form....

If it is core form use this hook
hook_form_FORM_ID_alter and replace form id for your form and try to print the form in the hook ....... Echo "pre"; Print_r($form) ; By printing you can know the structure of the submit button array format......and add ur custom submit handler in the same way and print your function whether it is added or not

If its custom form then add the same in the build form structure submit button code

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.