4

I have a taxonomy vocabulary that has the following terms: Green Red White

How would I go about creating a restful view for an endpoint that returns this list as an array of entity data for each term?

Chris Mitchell
  • 634
  • 10
  • 28
  • It's not clear whether you want to list taxonomy term entities, or the node entities tagged with those terms. Can you clarify? – osman Apr 12 '18 at 16:28
  • Both if possible? A term and then an array of all entities that have that term? Or just the taxonomy terms would work and then I could call a separate endpoint for each as needed. – Chris Mitchell Apr 12 '18 at 20:34
  • I was going to suggest using Drupal's Views with **REST export** display, however I realized `Serializer` format doesn't support grouping the output result. If you have [REST UI](https://www.drupal.org/project/restui) installed you can enable **Taxonomy term** resource. That's the endpoint for accessing individual term details. For example; GET `taxonomy/term/7?_format=json` would return the term details for (tid=7) – osman Apr 14 '18 at 16:51
  • This doesn't solve my issue as I need to query Drupal to get a list of terms, otherwise I don't know that /term/7 exists. – Chris Mitchell Apr 18 '18 at 14:43

2 Answers2

0

I think I have a solution for you:

JSON API module generates an API server that implements the {json:api} specification. It has video tutorials and good documentation with examples.

I skipped a few steps to Filtering documentation and tested out the following.

I have a Basic page content type with an Entity reference field (field_color) to Colors vocabulary -which has the terms: Green, Red, and White.

After playing around a bit, following GET request returned me the node entities tagged with color Red:

https://localhost/jsonapi/node/page?_format=json_api&filter[field_color.name][value]=Red

Hope this helps.

osman
  • 635
  • 4
  • 17
  • 1
    This does not solve my original issue, How would I know the value Red? I need to query Drupal taxonomy to get all of those terms. – Chris Mitchell Apr 18 '18 at 14:44
  • You can GET the list of taxonomy terms of the `Color` vocabulary like this: http://localhost/jsonapi/taxonomy_term/color?_format=json_api Then you can make the second query given in the answer to get the list of nodes tagged with that term. – osman Apr 18 '18 at 15:18
  • this link does not work: /jsonapi/taxonomy_term/color?_format=json_api - throws a page not found error – Chris Mitchell Apr 20 '18 at 18:43
  • Can you confirm vocabulary machine-name is “color” in your setup as well? Because that link works for me. Perhaps you need to rebuild the cache. – osman Apr 20 '18 at 19:00
  • I had to remove the _format=json_api parameter to make it work: http://localhost/jsonapi/taxonomy_term/color – Sascha Klatt Mar 12 '19 at 14:34
  • The parameter Drupal expects is `?_format=api_json`, not `?_format=json_api`, or you can get there using a header like `Accept: application/vnd.api+json` if you want. The name of the module is switched compared to the name of the format, easy to miss. – moopet Mar 21 '21 at 10:39
0

Create a new taxonomy term view. Add the "rest export" display. Set to fields and add any fields you want. Add a vocabulary filter (or any other filters you want).

You can't add vocabulary as a relationship/fields yet. That's in 8.6 (patched here).

If you want relationships, as per the comments on the OP, you'd use jsonapi. I can't comment on osman's post, but be wary of jsonapi module's limit to 50 values. If 50 is ok, you can try using sparse fieldsets to reduce the payload of the request.

simesy
  • 410
  • 3
  • 9