I created a custom module that I consume an external api. I have also created a custom form (its action points to controller url) which filters the data. However, when I press the search button and the controller is loading, despite I get the right results immediately the controller is reloading and consequently I loose the filtered results.

How I can stop this behaviour?

This is my code so far:

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $ada = $form_state->getValue('ada');
    $sector = $form_state->getValue('sector');
    $sub_sector = $form_state->getValue('sub_sector');

if ($sub_sector=='') {
} else {

    $datefrom = $form_state->getValue('datefrom');
    $dateto = $form_state->getValue('dateto');




class DiafaneiaController extends ControllerBase {

  public $filters=[];

  public function getList(Request $request) {

    $filters = \Drupal::request()->request->all();

    $data = array();
    $response = $data = $result = null;

    $page = pager_find_page()+1;
    //echo 'pageNo:'.$page;

    if (function_exists('diafaneia_reponse')) {
      $response = diafaneia_reponse('https://diafaneia.hellenicparliament.gr/api.ashx?q=documents&pageSize=20&pageNo='.$page.'&sector=0216896c-4d26-49de-b896-a2b600ab3843', 'GET',$filters);

    if ($response) {

      $result_docs = json_decode($response);
      $data = array();

      // # add all the data in one multiple dim array
      $data['title'] = 'products to consume REST API';
      $data['docs'] = $result_docs;

$num_per_page = 10;
$offset = $num_per_page * $page;

// Now that we have the total number of results, initialize the pager.
pager_default_initialize($result_docs->TotalRecords, $num_per_page);

$render = [];
$render[] = [
  '#theme' => 'diafaneia_list',
  '#data' => $data,
  '#type' => 'remote',

$render[] = ['#type' => 'pager'];

    return $render;

1 Answer 1


It's unclear how both pieces of code are connected with each other.

If you are handling the form values in form submit you need to pass the resulting parameters to the controller:

public function submitForm(array &$form, FormStateInterface $form_state) {
  // prepare $params
  $form_state->setRedirect('route_to_controller', [], ['query' => $params]);

If you are pointing the form action to the controller

$form['#action'] = Url::fromRoute('route_to_controller')->toString();
$form['#method'] = 'get';

then there is no form submit and you have to process the submitted values in the controller.

  • How can I change the form action and what should I write in? I need to pass the variables to the controller as you indicate but the action has the controller url by default. Commented Sep 30, 2020 at 7:29
  • See the second example. It's one way or the other, not both at the same time.
    – 4uk4
    Commented Sep 30, 2020 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.