0

I have written a custom WebformHandlerBase and attached it to the Mulitpage webform. With this I'm able to get the values into the custom handler as soon as user clicks on Next Page> button

class MyFormHandler extends WebformHandlerBase {
     public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

         $form_id = 'webform_submission_' . $webform_submission->getWebform()->id() . '_form';

         if($form_id == 'webform_submission_registration_content_form') {
            $values = $webform_submission->getData();
            /* custom code to calculate the score based on the values */
             $eligible_to_participate = 1;
             if ($score >= x && $score <= y) {
                  $eligible_to_participate = 1;
              }
             else
             {
                 $eligible_to_participate = 0;
             }
             if($eligible_to_participate == 0) {
                  // This user is not eligible so redirect to 'finish page'
                  /* The below code is not working 
                   * how-ever the same code is working in hook_form_FORM_ID_alter()
                   */
                  $url = Url::fromRoute('entity.node.canonical', ['node' => 78]); 
                  $response = new RedirectResponse($url->toString()); 
                  $response->send();
              }
          }
      }
 }

Based on the user selected values I need to redirect the user to a different page and the code for doing the same is as follows

 use Drupal\Core\Url;
 use Symfony\Component\HttpFoundation\RedirectResponse;

 $url = Url::fromRoute('entity.node.canonical', ['node' => 78]);
 $response = new RedirectResponse($url->toString());
 $response->send(); 

Now the problem is, this code is not working in custom handler function. If I write the same code in hook_form_FORM_ID_alter() it is working fine.

Any idea where am I doing wrong? Any help much appreciated.

  • If you want to alter the confirmation redirect you will need to use \Drupal\webform\Plugin\WebformHandlerBase::confirmForm. – jrockowitz Sep 6 '17 at 3:22
  • Hi jrockowitz, I just moved the RedirectResponse code to WebformHandlerBase::confirmForm(), but still i can't redirect to the configured page. I found this thirdandgrove.com/redirecting-node-pages-drupal-8 but I'm not sure how to pass the GetResponseEvent to the redirectMyContentTypeNode() from confirmForm() . Any further thoughts? – rdev Sep 6 '17 at 5:09
  • The link is for a event subscriber. You can't use this code in a form. And never send a RedirectResponse directly as you've done in the question. In a form use FormState::setRedirect – 4k4 Sep 6 '17 at 6:26
  • Hi 4k4, thank you!! It works like a charm. I appreciate it. – rdev Sep 6 '17 at 21:40
  • @rdev in my case I definitely need to find a way to make it work in WebformHandlerBase as I'm creating an Entity and I need its ID for a redirect. Dit you discovered how can you do it in an instance of WebformHandlerBase instead form_alter ? I think I would have to alter the /admin/structure/webform/manage/xxx/settings/confirmation behavior because it will always do a redirect. – hugronaphor Oct 20 '17 at 18:29
5
<?php
namespace Drupal\sa_webform\Plugin\WebformHandler;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;

/**
 * Form submission handler.
 *
 * @WebformHandler(
 *   id = "my_webform_handler",
 *   label = @Translation("My webform handler"),
 *   category = @Translation("Webform Handler"),
 *   description = @Translation("Calculate the score with webform 
 *                                                     submissions"),
 *   cardinality = 
 *       \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = 
 *    \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 * )
 */

 class MyWebformHandler extends WebformHandlerBase {
 /**
  * {@inheritdoc}
  */
    public function submitForm(array &$form, FormStateInterface $form_state, 
                           WebformSubmissionInterface $webform_submission) {

       $form_id = 'webform_submission_' . $webform_submission->getWebform()-
               >id() . '_form';
       if($form_id == 'webform_submission_xxx_form') {
         $values = $webform_submission->getData();  
         /* Take the action based on the Webform submission values */   
       }    
    }

    public function confirmForm(array &$form, FormStateInterface 
          $form_state, WebformSubmissionInterface $webform_submission) {
       $form_id = 'webform_submission_' . $webform_submission->getWebform()-
                                                  >id() . '_form';
       if($form_id == 'webform_submission_xxx_form')
       {
          if(based_on_some_condition) {
          /* Here 12 is the node id where i wanted to redirect to */
          $form_state->setRedirect('entity.node.canonical',['node' => 12]);
       }
    }
}   
?>

For more examples refer webform modules src code located at webform\src\Plugin\WebformHandler

BrokenWebformHandler.php

DebugWebformHandler.php

EmailWebformHandler.php

RemotePostWebformHandler.php

  • Welcome to Drupal Answers! May you explain the code you wrote? Keep in mind Stack Exchange is for helping also future users, not just the user who asked the question. – kiamlaluno Oct 22 '17 at 11:08
  • The helpful point in this answer is that you need to setRedirect() in the confirmForm(). – Duncanmoo Feb 12 '18 at 7:54

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.