4

I have a Form route in a site I am working on that is doing an entityQuery and displaying tabular data with some form elements to provide filtering, but without paging. For various reasons I can't use Views to do this; a lot of the table cells are complex calculations and don't lend themselves to a proper plugin.

$query = \Drupal::entityQuery('node')
  ->tableSort($headers)
  ->condition('type','foo')
  ->condition('status', Node::PUBLISHED);

if ($filters) {
  // Add more conditions.
}

$results = $query->execute();

and then I essentially just have

public function submitForm(array &$form, FormStateInterface $form_state) {
  $form_state->setRebuild(TRUE);
}

public function resetForm(array &$form, FormStateInterface $form_state) {
  $form_state->setRebuild(FALSE);
}

as my form handlers.

The table sorting works great. My form and filters work great.

Here's my problem. When a user clicks on a table header, it is essentially a link to the page w/ a GET query with the sort selection and bypasses the form submission process. When this happens, the filters get reset. Can TableSort play nicely and do a proper form submission, or do I need to cookie the form state and do the rebuild manually (as needed).

5
  • 2
    You could add a few lines of JS to listen for the header click, add the sort param to the form action, then submit the form as a normal POST?
    – Clive
    Apr 7, 2020 at 15:21
  • Can TableSort play nicely and do a proper form submission are you saying that if you remove ->tableSort($headers) the rebuild works as expected?
    – No Sssweat
    Apr 8, 2020 at 2:49
  • 1
    @Clive, since Drupal\Core\Render\Element\Table is a FormElement it should be able to produce such POST links on its own. Looks to me like this is a valid bug report or feature request.
    – 4uk4
    Apr 8, 2020 at 8:44
  • @4k4 Views exposed filters + table output work together, but those are all GET forms, and the exposed filters and table are different <form> elements. So, I think this is just a problem with my form and not a bug,
    – mpdonadio
    Apr 8, 2020 at 15:08
  • Now the table sort feature is added in a preprocess hook and it doesn't behave differently than a render element. As a form element preparing the clickable headers should probably start in a #process callback.
    – 4uk4
    Apr 8, 2020 at 15:36

0

Your Answer

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

Browse other questions tagged or ask your own question.