Get access to the View row count in the exposed filter block.

Created on 6 December 2023, about 1 year ago
Updated 12 April 2024, 8 months ago

I am using BEF 6.0.2 with Drupal 9.5. I have a fairly simple view setup with some exposed filters. I have set the exposed filters to display in a block. One of the exposed filters the user can adjust is a drop-down to select how many records to show on the page at a time. The requirements from the client is to show a drop-down that says "View {dropdwon of options} of 2,286 results." It's the "of 2,286 results" part that is proving to be difficult. I have a TWIG template setup for the exposed filter called "views-exposed-form--bibliography-search.html.twig". This lets me position stuff where I want it, but for some reason I just can't get access to the main View row count. If I dump "_context", I can see the record count buried in the "form" key, but if I try to drill down to it, when I get to the "#view" key, it throws an error in PHP if I try to access it in a preprocess function (theme_preprocess_views_exposed_form__bibliography_search), and in TWIG it just comes back as null. Is there a better/correct way to just get the row count of the main view in my exposed filter template?

THANKS

πŸ’¬ Support request
Status

Closed: outdated

Version

6.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States socalerich

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @socalerich
  • πŸ‡ΊπŸ‡ΈUnited States socalerich

    I'm not sure if there is a better way to do this, but what ended up working for us was to add a preprocess function to our .theme file like this:

    function YOUR_THEME_preprocess_views_exposed_form(&$variables) {
      if(isset($variables['form']['#view']) && $variables['form']['#view']->total_rows) {
        $variables['total_rows'] = $variables['form']['#view']->total_rows;
      }
    }
    
  • Status changed to Closed: outdated 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks for reporting, I think your solution should be just fine.

  • πŸ‡¬πŸ‡§United Kingdom gsquirrel London

    I tried that solution but it did not work for me - and there was no "#view" visible in kint under $vars['form']

  • πŸ‡ΊπŸ‡ΈUnited States socalerich

    Did you remember to replace "YOUR_THEME" with the actual name of your theme in the function declaration?

  • πŸ‡¬πŸ‡§United Kingdom gsquirrel London

    Yes it is using my theme name, and the function itself is working but no data comes through to "total_rows" - as I say if I add kint to inspect the available variables (into the preprocess function) then I can see $vars['form'] expanded with all the available data - but no sign of "view" or anything remotely similar - only the exposed form data itself..

    My form is for a search_api index view - maybe this makes a difference?

Production build 0.71.5 2024