How to display the number of elements of a view with twig?

I created a view of 50 products with a pagination (12 products per page).

If I type the following code:

{{ drupal_view_result('boutique_page_produit', 'block_2')|length }}

This displays the number of items in the view's page (12). I want the total element number of my element (50).

How to do this ?

One solution would be to create a second view without pagination, but is there another solution ?

  • What’s wrong with using the result summary in the header of the view? – Kevin Mar 19 '18 at 19:27
  • @Kevin I need to display only the number of items in a view in another view. – user84097 Mar 19 '18 at 19:32
  • @Kevin If not, what is the twig code to display only the summary of the results? (without displaying the entire view) – user84097 Mar 19 '18 at 19:40
  • 1
    I don't think this is doable in twig alone - I think you would have to preprocess it to execute the view, and get the result count, then pass that in as a new variable. – Kevin Mar 19 '18 at 19:46
  • @Kevin Thank you. So the only solution is to create another view without pagination. – user84097 Mar 19 '18 at 19:55

The total items is available in views.view.html.twig like:

{{ view.total_rows }}

You also can use the views query and execute a ->count() on it :

$total_rows = $view->query->query()->countQuery()->execute()->fetchField();

Your Answer

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