It is possible to count the contents associated with a nested taxonomy in Drupal 8 views, but the difference of this is that the taxonomies are of two levels.

For example:

I have these taxonomies:

enter image description here

Now, I have three created contents in which I associated the terms of the taxonomy, I stay like this:

I need something like this: enter image description here

What I need to get in the view is, the total post associated with the parent taxonomy.

For example, I would like to see something like:

Backend: 2

Frontend: 1

What I have tried so far only shows me the totals independently(using aggregation), for example:

Backend - Post One - Total: 1

Ruby- Post One - Total: 1

Backend - Post Two- Total: 1

Python- Post Two- Total: 1

Frontend- Post Other- Total: 1

Is it possible to do this in Drupal 8 views?


I tried something similar I think, here's my code but keep in mind it's not perfect.

first get alle the terms

$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$nodes = $node_storage->loadMultiple();
$node_tags = [];
foreach ($nodes as $node) {
  if ($node->hasField('field_terms')) {
    foreach ($node->get('field_terms')->getValue() as $value) {
      array_push($node_tags, $value);

after that you count all the terms

foreach ($node_tags as $key) {
  if (isset($count[$key['target_id']])) {
  else {
    $count[$key['target_id']] = 1;

then (and this still needs a lot of work) you search for them

$tags = $terms_storage->loadTree('tid');
foreach ($count as $key => $c) {
  foreach ($tags as $tag){
    if($tag->tid == $key){
     //here you can get the $tag->tid
     //you can get $tag->name
     //time used = $c

if you want to get the other terms

foreach ($tags as $tag){
   //here you get the same info

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.