0

My nodes contain an entity reference field, Ref Term. I want to fetch a list of nodes that have a specific term with jsonapi.

In jsonapi, when I fetch nodes, the field with the taxonomy term reference is returned like this:

"field_ref_term": {
"type": "taxonomy_term--my_vocabulary",
"id": "b1a97358-9834-499d-aee0-ebb0302fb2c3"
},

So, first, I tried this:

/jsonapi/node/my_content_type?filter[field_ref_term]=f9c171db-1a1d-4d9a-bb55-5b0f8b033749

However, that gave me an error:

"title": "Bad Request",
"status": "400",
"detail": "Invalid nested filtering. The field `field_ref_term`, given in the path `field_ref_term` is incomplete, it must end with one of the following specifiers: `id`.",

So I tried adding [id]:

/jsonapi/node/my_content_type?filter[field_ref_term][id]=f9c171db-1a1d-4d9a-bb55-5b0f8b033749

That gave me a different error:

"detail": "You must provide a valid filter condition. Check that you have set the required keys for your filter.",

So I tried a longer version:

/jsonapi/node/my_content_type?&filter[term-filter][condition][path]=field_ref_term&filter[term-filter][condition][id]=f9c171db-1a1d-4d9a-bb55-5b0f8b033749

That gave me the first error again. I read the documentation on filtering.

How can I filter my nodes based on which are tagged with a specific taxonomy term? (In my case, each node has one and only one taxonomy term in its entity reference field.)

2

You were close, but your syntax was off. This is documented further down in your linked page, in the Filter Examples > Get nodes by a value of a entity reference section.

SHORT
filter[uid.id][value]=BB09E2CD-9487-44BC-B219-3DC03D6820CD

NORMAL
filter[author-filter][condition][path]=uid.id
filter[author-filter][condition][value]=BB09E2CD-9487-44BC-B219-3DC03D6820CD

So, in your example, you'd need to use:

SHORT
/jsonapi/node/my_content_type?filter[field_ref_term.id][value]=f9c171db-1a1d-4d9a-bb55-5b0f8b033749

LONG
/jsonapi/node/my_content_type?filter[term-filter][condition][path]=field_ref_term.id&filter[term-filter][condition][value]=f9c171db-1a1d-4d9a-bb55-5b0f8b033749

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.