1

I have a custom form built with the Form API. The general flow is:

  1. Build and display form
  2. Get user input
  3. Rebuild and form
    • query based on user input with added

      $query = $query
        ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
        ->limit($values['items']);
      

This all works well for the first submission and for paging between submissions, but when a user enters a different value for the query, while the search results change, the pager does not update (it displays the same number of result pages as with the old query and stays on the same page of the query.

So if the first query (e.g. "duck") resulted in 14 pages of results, and I am on the 3rd page of duck results, when I change the query field to "chicken" and submit, the actual results show the right things, but the pager still shows 14 pages instead of the 35 for chicken and it keeps me on the 3rd page instead of taking me to the first page).

How do I force the pager to update?

1 Answer 1

2

I figured it out, hopefully this will help someone else with a similar question.

Instead of doing a $formstate->setRebuild(); in the submitForm method, I changed it to

$form_state->setRedirect(
    'sliker_drupal_backend.browser',
    [],
    [
      'query'=>[
        'logic'=>$form_state->getValue('logic'),
        'items'=>$form_state->getValue('items'),
        'phrase'=>$form_state->getValue('phrase'),
        'visibility'=>$form_state->getValue('visibility'),
        'ids'=>$form_state->getValue('ids'),
      ],
    ]);

the setRedirect() method takes three arguments:

  • Route machine name
  • Arguments (to fill in "placeholders" in the route path)
  • Options which are any options that can be passed to a \Drupal\Core\Url

By searching code, found options is an associative array and the keys can be any of:

  • 'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
  • 'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
  • 'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
  • 'attributes': An associative array of HTML attributes that will be added to the anchor tag if you use the \Drupal\Core\Link class to make the link.
  • 'language': An optional language object used to look up the alias for the URL. If $options['language'] is omitted, it defaults to the current language for the language type LanguageInterface::TYPE_URL.
  • 'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP.

I then updated my logic in the form build to look for query parameters and to use that information if it is present.

See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Form!FormState.php/function/FormState%3A%3AsetRedirect/8.9.x for more information on setRedirect().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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