4

Somebody asked Views and node count for taxonomy terms. I am trying to do the same, but with Drupal 8.

I follow the steps reported in that question, and I get a count of the taxonomy term (say "fruit"), but when I click on it there is a page that lists "fruit" multiple times, instead of listing the pages themselves.

If I do not do a count. then when I click on the taxonomy term I get the correct listing page (e.g. apple, orange, pear).

Can anyone help me with this?

12

The following worked for me in Drupal-8.0.6.

Note that for some reason 'Content NID' is not offered as a field for the 'Content with term' relationship, so I used UUID instead and it worked.

  1. Create a Taxonomy Term view (with a field Taxonomy term: name, usually linked to the term page)
  2. Relationship > Taxonomy term: Content with term (will be called node).
  3. Advanced: Use aggregation: Yes
  4. Add Field > Content: UUID with relationship node
  5. Aggregation type for UUID = Count

You may have to play with COUNT vs COUNT distinct

If you want tbe count to appear in brackets like this:

linked_term (count)

On UUID field use Rewrite results:

({{ uuid }})

Note that it is using the new Twig template language notation.

3

Differently from what stated in the other answer for Drupal 8.0.6, there is a field for the node ID, in Drupal 8.1.3.

With Drupal 8.1.3, I followed these steps:

  • I created a view of taxonomy terms, limiting them to the ones of just a vocabulary (in my case Blog categories)
  • As relationship, I added Content using field_blog_category, where field_blog_category is the field I added to a content type I created
  • I checked Use aggregration
  • I added ID (listed under the Content category) as new field
  • I set Aggregration type to Count for the new field (by clicking on Aggregration settings for that field)

What I got was the following one (as seen from the preview).

screenshot

Following the steps I listed, but using UUID as new field (still listed under the Content category), I would have obtained the same result.

As expected, also the resulting query didn't change much, using the node ID or its UUID.

Using the node ID

SELECT taxonomy_term_field_data.name AS taxonomy_term_field_data_name, COUNT(field_blog_category_taxonomy_term_field_data.nid) AS field_blog_category_taxonomy_term_field_data_nid, MIN(taxonomy_term_field_data.tid) AS tid, MIN(field_blog_category_taxonomy_term_field_data.nid) AS field_blog_category_taxonomy_term_field_data_nid_1
FROM 
{taxonomy_term_field_data} taxonomy_term_field_data
LEFT JOIN {node__field_blog_category} node__field_blog_category ON taxonomy_term_field_data.tid = node__field_blog_category.field_blog_category_target_id AND node__field_blog_category.deleted = '0'
LEFT JOIN {node_field_data} field_blog_category_taxonomy_term_field_data ON node__field_blog_category.entity_id = field_blog_category_taxonomy_term_field_data.nid
WHERE (( (taxonomy_term_field_data.vid IN  ('blog_categories')) ))
GROUP BY taxonomy_term_field_data_name
LIMIT 21 OFFSET 0

Using the node UUID

SELECT taxonomy_term_field_data.name AS taxonomy_term_field_data_name, COUNT(field_blog_category_taxonomy_term_field_data__node.uuid) AS field_blog_category_taxonomy_term_field_data__node_uuid, MIN(taxonomy_term_field_data.tid) AS tid, MIN(field_blog_category_taxonomy_term_field_data.nid) AS field_blog_category_taxonomy_term_field_data_nid
FROM 
{taxonomy_term_field_data} taxonomy_term_field_data
LEFT JOIN {node__field_blog_category} node__field_blog_category ON taxonomy_term_field_data.tid = node__field_blog_category.field_blog_category_target_id AND node__field_blog_category.deleted = '0'
LEFT JOIN {node_field_data} field_blog_category_taxonomy_term_field_data ON node__field_blog_category.entity_id = field_blog_category_taxonomy_term_field_data.nid
INNER JOIN {node} field_blog_category_taxonomy_term_field_data__node ON field_blog_category_taxonomy_term_field_data.nid = field_blog_category_taxonomy_term_field_data__node.nid
WHERE (( (taxonomy_term_field_data.vid IN  ('blog_categories')) ))
GROUP BY taxonomy_term_field_data_name
LIMIT 21 OFFSET 0
3
  • Is there some new way to do this in drupal 8.3.7? Thank you in advance. :)
    – SakaSerbia
    Oct 4 '17 at 16:30
  • You need to edit relationship in advanced setting. May 13 '18 at 23:11
  • How can i rewrite the output of this to taxonomy term(count) ?
    – Yukare
    Jul 19 '18 at 21:44
0

Ran into a Drupal core bug with the taxonomy Content with term relationship. Patch in https://www.drupal.org/project/drupal/issues/2674832#comment-10890660 Let me use other content fields to filter (content type, not just UUID) to get a count.

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.