Obtain the total (count) of the contents associated with a nested taxonomy (only in the parent taxonomy) 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:



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

I need something like this:


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()-&gt;getStorage('node');
$nodes = $node_storage-&gt;loadMultiple();
$node_tags = [];
foreach ($nodes as $node) {
  if ($node-&gt;hasField('field_terms')) {
    foreach ($node-&gt;get('field_terms')-&gt;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']])) {
    $count[$key['target_id']]++;
  }
  else {
    $count[$key['target_id']] = 1;
  }
}
arsort($count);


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

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


if you want to get the other terms

foreach ($tags as $tag){
  if(!array_key_exists($tag-&gt;tid,$count)){
   //here you get the same info
  }
}