1

I am acutally trying to build a custom webform submit handler to force the form to be rebuilded. I know that this is possible with the integrated webform settings, but I need to do it programmatically.

Here is what my code looks like at the moment:

function he_helper_form_alter(&$form, &$form_state, $form_id) {
    dsm($form);
    dsm($form_state);
    dsm($form_id);

    if(strpos(current_path(), 'print') !== false && $form['#node']->type == 'genericforms') {
        $form['actions']['submit'] = array (
            '#type' => 'submit',
            '#value' => 'submit',
            '#submit' => 'he_helper_custom_redirect'
        );
    }
}


function he_helper_custom_redirect($form, &$form_state) {

    $form_state['rebuild'] = TRUE;

}

The problem now is, that the rebuild works as expected, but the form data does not get submitted. I tried it with $form_state['redirect'] as well, but ran into the same problem.

Anyone got an idea how to solve that?

UPDATE:

function he_helper_form_alter(&$form, &$form_state, $form_id) {
  // dsm($form);
  // dsm($form_state);
  // dsm($form_id);
  if(strpos(current_path(), 'print') !== false && $form['#node']->type == 'genericforms') {
    // $form['#after_build'][] = 'he_helper_after_build';
    $form['actions']['submit'] = array (
      '#type' => 'submit',
      '#value' => '2hello',
      '#submit' => array('_he_helper_custom_submit'),
    );
  }
}

function _he_helper_custom_submit(&$form, &$form_state) {
  $form_state['redirect'] = 'print';
  $form_state['rebuild'] = TRUE;
}

This is what my code now actualy looks like. The redirect now works fine, but the data wont get submitted. I tried the after_build function as well, but when i am using this, even my redirect wont work. This is what my code looked like with the after_build

function he_helper_form_alter(&$form, &$form_state, $form_id) {
  // dsm($form);
  // dsm($form_state);
  // dsm($form_id);
  if(strpos(current_path(), 'print') !== false && $form['#node']->type == 'genericforms') {
    $form['#after_build'][] = 'he_helper_after_build';
  }
}


function he_helper_after_build(&$form, &$form_state) {
  $form['actions']['submit'] = array (
    '#type' => 'submit',
    '#value' => '2hello',
    '#submit' => array('_he_helper_custom_submit'),
  );
  return $form;
}

function _he_helper_custom_submit(&$form, &$form_state) {
  print 'hello';
  dsm('hello');
  $form_state['redirect'] = 'print';
  $form_state['rebuild'] = TRUE;
}
2
  • If you want to submit the data what is the purpose of re-builidng the form?
    – Felix Eve
    Mar 12, 2014 at 13:53
  • I just want to get redirected to the form itself, but as I wrote, i ran into the same problem with $form_state['redirect'].
    – Sebastian
    Mar 12, 2014 at 14:04

1 Answer 1

2

I think I've just used $form_state['redirect'] in the submit handler in the past.

However I'm not sure what order your submit handlers are firing in, maybe it would work if you added the after_build function to the form array as opposed to the button array. (If your custom submit handler is firing first then your redirect could get overwritten.)

E.g: $form['#submit'][] = 'he_helper_custom_redirect';

If that doesn't work then this post suggests using rebuild with redirect:

$form_state['redirect'] = 'where-do-you-want-to-go-today';
$form_state['rebuild'] = TRUE;

This code shows how to add a new custom submit handler without overwriting the original one:

function he_helper_after_build(&$form, &$form_state) {

  // add to the existing submit array to not override the default submit handler
  $form['actions']['submit']['#submit'][] = '_he_helper_custom_submit';

  // change the button text
  $form['actions']['submit']['#value'] = '2hello';

  return $form;
}
5
  • Thanks for your reply. I was wrong with my adoption that the "redirect" was working, cause it was not. I dont know why, but the redirect wont work whatever I try... I now tryied as well the after_build function, but it seems like that webforms is using that as well and when I use it, the form disappears :-/
    – Sebastian
    Mar 13, 2014 at 6:52
  • You need to put return $form at the end of your after build function or the form will disappear!
    – Felix Eve
    Mar 17, 2014 at 8:58
  • Updated my top post
    – Sebastian
    Mar 18, 2014 at 12:50
  • Your data isn't getting submitted because you are overwriting the default submit handler attached to the submit button (and that is where the data gets saved). Inspect the form array before making any changes to the form and see what the submit handler attached to the submit button is. Then when you make you changes make sure to call the default submit handler as well as your custom one.
    – Felix Eve
    Mar 18, 2014 at 14:17
  • I've also updated my answer to show how to add a submit handler without overwriting the existing one.
    – Felix Eve
    Mar 18, 2014 at 14:19

Your Answer

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

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