0

Goal: Using webform (and it's hooks) in drupal 7 I need to, under certain conditions, avoid saving the form submission (i.e. under those conditions I need to discard the submission instead of saving it to submissions models or, if that's not possible, correctly reference the submission and delete it).

Question: How is that done? Either of the solutions would be fine for me. I didn't try anything yet, since I have no clue at all about how to do any of them. I can just barely imagine it should be done on _alter or _presave hooks, but I'm not sure, and can't find about.

Current state: Totally n00b and clueless about.

Notes: Assume hooks as:

function mymodule_form_alter(&$form, &$form_state, $form_id)
{
    //validations occur here to test whether the current form is a webform node.
}

EDIT - Additional IMPORTANT notes I forgot: Depending on the value of some fields, I have two possible scenarios:

  1. Avoid saving, then redirect (to external URLs, i.e., urls not belonging to my Drupal install. The redirection url must be the result of an evaluated expression, and not just a single url which I could specify in the webform settings).
  2. Save, then redirect (to internal URLs, i.e., urls belonging to my Drupal install. The redirection url must be the result of an evaluated expression, and not just a single url which I could specify in the webform settings).
  • when you say under certain conditions only, do you mean form validation? – arpitr Apr 1 '14 at 16:33
  • No. Scenario: I need the form passes validation. After validation, I check a value. If the value is "X" (say), I must save the form. If it is "Y" or "Z" I want to redirect, based on other form's values, and NOT save the form. – Luis Masuelli Apr 1 '14 at 16:39
0

yes hook_form_alter is the right hook you are looking for.

Something like below. Please check the values of form field inside $form_state and fix the code accordingly.

Basically hook_form_alter will allow you wo conditionally add submit handler to your webform which means if form field value is Y then only your custom submit handler will call, also the way we adding the custom submit handler is by replacing the other handlers so no conflict with your sudmit handler and any other comes by default.

function mymodule_form_alter(&$form, &$form_state, $form_id)
{
   if($form_id == "your_webform_id") {
     if($form_state['your_form_field']['#value'] == 'Y') {
       $form['#submit'] = 'custom_submit_handler_external';
     }
     if($form_state['your_form_field']['#value'] == 'X') {
       $form['#submit'][] = 'custom_submit_handler_internal';
     }
   }
}

function custom_submit_handler_external(&$form, &$form_state) {
  drupal_goto("redirect to a page");
}

function custom_submit_handler_internal(&$form, &$form_state) {
  $redirect_to = $form_state['redirect_field']['#value'];
  $form_state['redirect'] = $redirect_to;
}

The above code is just an idea of how you can approach, please debug it at every step and make it workable for you.

  • Redirecting avoids saving? I need Two scenarios: 1. redirect (and no save), 2. save, then redirect (to an internal Drupal page, e.g. another node) – Luis Masuelli Apr 1 '14 at 17:18
  • Sorry, did not explain it before, and the difference seems critical. will update question now. – Luis Masuelli Apr 1 '14 at 17:18
  • question updated with "additional important notes". – Luis Masuelli Apr 1 '14 at 17:23
  • @LuisMasuelli check the updated answer – arpitr Apr 1 '14 at 19:32
  • Ty :D. [redirect_field][#value] expects drupal-only q= urls? – Luis Masuelli Apr 1 '14 at 19:34
0

I had the certain scenario where I had to prevent webform save for a particular component namely 'pay' component. I could achieve this with the following code

function hook__webform_submission_presave($node, &$submission) { 
  $components_list = $node->webform['components'];
  foreach ($components_list as $comp)
  {
   if($comp['type']=='pay')
   {
    $comp_id = $comp['cid'];
   }
  }
 unset($submission->data[$comp_id]); 
}

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.