I'm building a index view to use as search. I'm grouping the results by a term field and everything works fine. What I want to do is add a count after the group title to show how many results that exists with this term.

Can't find a way to do this in drupal 8 so I'm asking you. I've google for half a day without finding anything.


Using this answer as a base you can add views_field_view to include a view of content filtered by term.

In more details

1. Create a view of terms that includes count of content using each term

  • Create a view that displays terms
  • enable aggregation
  • add relationship Content with term
  • add sort content uuid with aggregation type: count (optional)
  • add field content uuid with aggregation type: count
  • add field taxonomy term

Your view should look similar to the following

enter image description here

2. Create a view that will display the content

  • Include contextual filter on the vocabulary of your interest (tags in this case)
  • Add any filters or fields that you need

This view will look similar to

enter image description here

3. Add last view as a field to first view (step 1.)

  • Download and enable views_field_view module
  • Go to view from step 1. and
    • add field Taxonomy term: Term ID
    • add field Global view ) (made available from views_field_view)
    • on the settings find the view from step 2.
    • use {{ raw_fields.tid }} as contextual filter

Next screenshot shows the changes from original view and settings for view field

enter image description here

You are done

Your view now should display the terms with count and all the content that have used it.

  • Sorry, forgot to mention that it is a "Index" view, so the "Use aggregation" option is not available. – Daniel Mar 22 '19 at 8:34

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.