1

I have a taxonomy with 2 level of depth and I have a view that get some documents. In this view I can filter by the type of document (get from the taxonomy) but only by the first level.

The problem is that my documents returned by the view query are only those who are tagged with exactly the type choosed, and not his type childs. I want to get all the documents of the type choosed and the childs type.

So I try to use the views_query_alter :

function mymodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
   if ($view->id() == 'resource_center') {
      // Get the taxonomy
      $catalogue_taxo = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('catalogue');
      foreach ($query->getWhere() as &$condition_group) {
          foreach ($condition_group['conditions'] as &$condition) {
              // If the condition if document family
              if($condition[0]=='field_family') {
                  $parent_term = $condition[1][0];
                  // Get all the child taxo of the selected element
                  foreach($catalogue_taxo as $term){
                    $parents = array();
                    $parents[$term->tid] = $term->parents;
                      if (in_array($parent_term, $term->parents)) {
                          $condition[1][] = $term->tid;
                      }
                  }
              }
          }
      }
  }
}

This works in the fact that my output query have the taxonomy id of the selected document type and all his childs. But this doesn't work in the fact that the query still return me only documents related to the first level type (the one who is selected)

What am I doing wrong ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy