0

Drupal 8.7.x

I have a custom form that currently replaces the form with the output of the form like so:

CalculationForm.php

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

    ...

    // Result overview.
    $result = [
      '#theme' => 'THEME_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);

    // Show rendered results.
    $this->response->addCommand(new ReplaceCommand('#form-calculation-container', $results_overview));
}

Currently the form submission makes an ajaxcall that replaces the container which holds the form with the rendered output (Replacecommand). Because we can't track the form submission with GTM I'm switching over to a normal form submission which redirects to a page.

My question is, how can I pass $results_overview to the redirect page and make it render as part of the content variable.

Thanks in advance

1 Answer 1

3

in your submitForm function you need to set rebuilt to true (the default is false) on the FormStateInterface then add the result as a value to the form_state. And in the buildForm function you need to check for the presence of the results variable and accordingly you can add it to the form. Something like this:

  public function buildForm(array $form, FormStateInterface $form_state) {

    ...

    $result = $form_state->getValue('result');
    if($result){
      $form['calculation'] = $result;
    }

    ...

    return $form;
  }

and

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

  ...

  // Result overview.
  $result = [
    '#theme' => 'THEME_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,
  ];

  $form_state->setRebuild();
  $form_state->setValue('result', $result);

}

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.