Drupal 8.7.x

I have form that passes the outputted data into a template. This template with the data gets rendered into a string like so: :

  public function submitForm(array &$form, FormStateInterface $form_state) {


        // Result overview.
        $result = [
          '#theme' => 'THEME_price_calculation_overview',
          '#name' => !empty($values['company_name']) ? $values['company_name'] : $form_state->getValue('firstname') . ' ' . $form_state->getValue('surname'),
          '#product' => $product,
          '#address' => $address,
          '#results' => $result_clean,
          '#urls' => $product_urls,

        // Bundle (rendered) results.
        $results_overview .= render($result);

        // Redirect to results page.
        global $base_url;
        $uri = Url::fromRoute("THEME_forms.price_calculation_results");
        $url =  $base_url . $uri->toString();
        $this->response->addCommand(new RedirectCommand($url));

I redirect to the route defined in my routing file using the ajax RedirectCommand after I submit the form.


  path: '/private/price-calculation-results'
    _controller: '\Drupal\THEME_forms\Controller\RedirectController::redirectCalculationResults'
    _title: 'Price calculation results'
    _access: 'TRUE'

My question is: How do I pass the rendered markup ($results_overview) to the redirect page defined in my routing file ? Would this be easiest by creating a custom ajax command and if so, how do I go about making said command ?

  • This is not possible, you can't post data via a redirect and it doesn't make much sense, AJAX is supposed to manipulate the DOM without reloading the page. Now since AJAX is basically js code you trigger server side you can do anything you want, also post data from the client back to Drupal. – 4k4 Oct 8 '19 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.