I've created a view in order to override the default search page form and results, then I've exposed the content type in order to have the possibility to filter by that, all now is working

How can I have the count of the relative content near each content type label on the filters?


Sounds like you want to use search facets. Facets are ways of showing various search 'tag' information for the current results with/without counts. Note that this is slightly different from a filter which will show all values even if there is no result with the specified search criteria.

That said, there is a defunct facets core search module. As the module page says, the best way to approach this with Drupal 8 is to use the Facets module which requires the Search API module with the included Search API DB engine (no Solr required).

This will add a bunch of extra functionality that the core search will not handle. Stuff like full text search across multiple text fields, stemmed searches (e.g. search for work also finds working, works, and the like), ability to show extracts (were the word was found), and much more.

| improve this answer | |

One way to create a filtering system that counts the results per category is by creating a Contextual filter for the type with the Summary option.

This will list all the types as links, and show the number of results next to each type name. When you click through those links you will see the results for that one filtered type under the Format you set in Views.

You can list all the results below the Summary if you use different displays as Blocks/Attachments and display them together. There are many different combinations how to configure this, details depend on your particular needs.

Another way to count the results according to some categorization is with Aggregation. When you turn it on in your Views all fields and filters get the neutral "group" setting. The field related to your rows (node ID, title, etc) can then be set to Count under Aggregation settings which will display the number instead of the actual field output. You have to be careful about the other fields you show because they can "break" the aggregation into individual rows instead of collapsing into one value of the counted items.

Sorting can affect Aggregation negatively so remove sort criteria while building the Aggregation.

After you get the count numbers, you can maybe group by the type field (under Format), and you can Rewrite output of fields to show the count where you want it, again it all depends on your needs.

| improve this answer | |

I've found a solution with a different approach

With form_alter I've targeted the form and edited the filter labels, by adding a custom query and counting items from the search_index table with a left join to the node_field_data table in order to get the node type

| improve this answer | |

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.