0

I have a form with filter checkboxes, submit button, results in tableselect and pager.

When I set some checkboxes and press the submit button, a query should be reexecuted, data should be placed into the tableselect and pager should be updated, because new data size can be different with old one.

When I do it the first time, query is executed successfully, tableselect also is updated without problems. But the pager doesn't want to be updated. And to update the pager I have to press the submit button second time. But it occurs only I pressed the submit button after the page with the form has been loaded.

Could you clarify me, how can I update the pager after first submission of the form ?

Here is a fragment of my code with the tableselect and the pager.

$q = \Drupal::entityQuery('node');
        $q = $q->condition('type', 'my_node_type')
            ->condition('status', 1);

        $pager_params = [];
        if ($event_types != []){
            $q->condition('field_art_der_veranstaltung', $event_types, 'IN');
        }

        $events = $q->sort('created', 'DESC')->pager(10)->execute();

        $nodes = Node::loadMultiple($events);

        $rows = [];

        foreach($nodes as $node){
            $rows[$node->id()] = [
                'nid' => $node->id(),
                'title' => $node->getTitle(),
            ];
        };

        $form['nodes_container']['nodes'] = [
            '#type' => 'tableselect',
            '#title' => $this->t('Events'),
            '#options' => $rows,
            '#header' => $header,
            '#empty' => $this->t('No events found'),
        ];

        $form['nodes_container']['pager'] = [
            '#type' => 'pager',
        ];

1 Answer 1

1

Seemingly, I have fould the solution. Namely, to avoid it, I set the Element property for the pager explicitely.

Here are fragments of code.

$nodes = $q->sort('created', 'DESC')->pager(10, 0)->execute();

/// code here

$form['nodes_container']['pager'] = [
  '#type' => 'pager',
  '#element' => 0,
  '#parameters' => $pager_params,
];

Unfortunately, I didn't have a time to debug and find out why it didn't worked before but it works now. Newertheless, it works.

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.