0

I created WebformHandler where I need to get data entered in the form on submit. It's something like this:

<?php

namespace Drupal\xxx\Plugin\WebformHandler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Form submission handler.
 *
 *
 * @WebformHandler(
 *   id = "xxx",
 *   label = @Translation("xxxx"),
 *   category = @Translation("xxx"),
 *   description = @Translation("xxx"),
 *   cardinality =
 *       \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results =
 *    \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 * )
 */
class XxXx extends WebformHandlerBase {

  public function confirmForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

    $values = $webform_submission->getData();
    // Get data here
  }
}

I also have the Controller and defined route with template file. Controller looks like this:

<?php

namespace Drupal\xxx\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\xxx\Plugin\WebformHandler;

/**
 * Class SearchFlightsController.
 */
class XxXController extends ControllerBase {

  /**
   * xXx.
   *
   * @return array
   *   Return search parameters string.
   */
  public function xXx() {

    return [
      '#theme' => 'xxx',
      '#search_query' => $this->t('Test Value'),
    ];

  }

}

My question is how to pass the data that I get in WebformHandler on form submit to Controller that need to show the page with defined route?

  • 1
    Add route parameter(s) to the controller and set a redirect in confirmForm(), see drupal.stackexchange.com/questions/245285/…. You can pass submitted values or simply the submission id to retrieve this data later in the controller. – 4k4 Jan 31 at 12:36
0

I don't know why you want to pass the data to the controller, you can do anything with the data in the webformhandler code. or you can pass the data using the namespace declared on the controller. please check the following code. Inside the controller file place the below code that I have added new function to handle the webform data.

namespace Drupal\xxx\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\xxx\Plugin\WebformHandler;

/**
 * Class SearchFlightsController.
 */
class XxXController extends ControllerBase {

  /**
   * xXx.
   *
   * @return array
   *   Return search parameters string.
   */
  public function xXx() {

    return [
      '#theme' => 'xxx',
      '#search_query' => $this->t('Test Value'),
    ];

  }

  public function getwebform($formdata){
      /** place your code what want to do with $formdata*/
  }

}     

//After that inside the webformhandler code write the following code /** take the data from webform and save in an array variable $formdata then pass to the function defined inside the controller */

use Drupal\xxx\Controller\XxXController; $webfrmdata = new XxXController;
$webfrmdata->getwebform($formdata);

I have not tested but hope this code can help you

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.