3

In drupal 8.5 I have a taxonomy term list like this:

Term A (12 items)
  Term 1 (3 items)
  Term 2 (8 items)
  Term 3 (1 items)
Term B (3 items)
  Term 4 (1 items)
  Term 5 (2 items)

I want to use the json api to fetch all data within term B. So I expect a result set of 3 items. But when I do:

https://www.some-api.com/jsonapi/node/test?
  _format=api_json&
  filter[taxFilter][condition][path]=field_regio&
  filter[taxFilter][condition][operator]=IN&
  filter[taxFilter][condition][value][]=2 (=> id of term B)

I get a response with "data": [], This is probably because the term id of these nodes are set to the children (term 4 and 5).

But isn't drupal supposed to return all nodes in the children of term B?

Otherwise how can I get all nodes in Term A or B?

3
  • You can just tag those nodes with the Parent term too right?
    – Beebee
    Commented Jun 28, 2018 at 13:39
  • What is the point of the tree structure in taxonomy then? Some terms can have multiple parents, very hard for editors to know what all the parents are.
    – yeager
    Commented Jun 28, 2018 at 13:42
  • You don't have to force authors to do that, you can do it in a hook_form_submit programmatically too. But I think best way to go might be using Views, create a RESTful view that can work with hierarchical taxonomies, effectively replacing jsonapi for this call. But isn't drupal supposed to return all nodes in the children of term B? No, IIRC I don't think it works that way out of the box.
    – Beebee
    Commented Jun 28, 2018 at 13:48

1 Answer 1

0

The term-ID 2 belongs to "Term B", not to "Term 4" or "Term 5". To check if term belongs to Term B you may include the term parent and check if it has id 2:

https://www.some-api.com/jsonapi/node/test?
_format=api_json&
include=field_regio.parent
filter[taxFilter][condition][path]=field_regio.parent&
filter[taxFilter][condition][operator]=IN&
filter[taxFilter][condition][value][]=3 (=> id of term B)

This should work at least for the simple example provided it the question.

See also D.A. question How do you correctly filter on taxonomy terms to get all nodes matching term or parent term? for more details and potential issue with permissions.

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.