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()->getForm('Drupal\movies\Form\SearchForm');
$searchForm['searchQuery']['#value'] = $query;
return [
'#theme' => 'insert_movie_page',
'#form' => $searchForm,
'#query' => $query,
'#results' => $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 &$form, FormStateInterface $form_state) {
$search = $form_state->getValue('searchQuery');
$SearchController = new SearchController;
$results = $SearchController->searchMovies($search);
}