Send form result back to twig I created a module for searching values from external databases. My controller creates a form and send it to the custom twig page of my module.

The controller method that creates the form and send the data to the twig template looks like:

 public function customSearchPage($query, $results)
    {
         $searchForm = \Drupal::formBuilder()-&gt;getForm('Drupal\movies\Form\SearchForm');

        $searchForm['searchQuery']['#value'] = $query;

        return [ 
            '#theme' =&gt; 'insert_movie_page',
            '#form' =&gt; $searchForm,
            '#query' =&gt; $query,
            '#results' =&gt; $results,
        ];
    }


Now I created a Form using the Drupal 8 Form API. The submitForm method will send the inserted data to a compute class and gets a resultset back (array with multiple model objects). Now I need to send the resultset back to the twig template to show the results to the users. How can I achieve this behaviour?

SubmitForm method looks like:

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

      $search = $form_state-&gt;getValue('searchQuery');

      $SearchController = new SearchController;
      $results = $SearchController-&gt;searchMovies($search);
  }
In submit you have two choices.

Rebuild

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

  $form_state-&gt;setRebuild();
}


The form and the twig template the form is wrapped in will be rebuilt and you can display the result.

This is a great question from @AdrianCidAlmaguer to demonstrate how this works with a basic example how to display the sum of two numbers: How to show a string once you submit a form in a page reloading the page?

Redirect

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

  $form_state-&gt;setRedirect($route_name, $route_parameters, $options);
}


You redirect to a new page and can display the result there.