4

A little bit of context:

I am running a headless Drupal 8 with React in the front-end. All the data gets exposed through various endpoints.

Now i am trying to render a view in react, there was no problem in obtaining the data. But when i tried to get the exposed filters i ran into a problem (the filters are based on taxonomy terms). I expected to get the filter values like this:

value: {5:"Value one", 6:"Value two", 49:"Value three", 50: "Value four"}

Except i got the filter values like this:

value: {5:5, 6:6, 49:49, 50:50}

So it feels like instead of get the key and value i only get the keys... I am wondering if i am doing something wrong or there is another way of obtaining the exposed filter values.

The following code i am using to obtain the filters

$this->$view // ViewExecutable
$filters = [];
foreach ($this->view->filter as $filter) {
  if($filter->isExposed()) {
    $filters[] = $filter->options;
  }
}
    
2
  • Just a heads up for anyone copying this code. The foreach has a typo. It is parsing the values into "filer" and then trying to read them as "filter". And the foreach is loading array "filters" while the array defined outside its scope is "filter". Commented May 4, 2021 at 22:07
  • @DeveloperWeeks, thanks i fixed the example.
    – Nylsoo
    Commented May 6, 2021 at 10:50

1 Answer 1

1

I think everything is fine. You have to load the taxonomy term names manually like:

\Drupal\taxonomy\Entity\TaxonomyTerm::load($ids)

Or maybe you can do a custom query to load only the taxonomy term titles without any field data (because that is what you need).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.