Hi All,

Small question here,
I'm toying around with the JSON API and all works amazingly well.

Huge thanks for all effort into this amazing project.

1 question:

If I have an entity that contains a list text field (eg: field_status) with values
* Active
* Inactive

and I wanted to render a select list on my frontend when displaying that entity.
Is there a way that I can both retrieve the current value as well as all possible values?

So I can properly populate my select list.

Many thanks

Comments

guardian87 created an issue. See original summary.

Wim Leers’s picture

Glad to hear it's working "amazingly well" for you :) Thanks for letting us know!

It totally makes sense that you want to retrieve the list of possible/allowed/valid values for a "list" field. We've got a long-existing feature request for this: #2822768: [PP-1] Add information about the schema in the json:api resource. In #2822768-11: [PP-1] Add information about the schema in the json:api resource I linked to the issue in the JSON:API spec where this is being discussed too. The key reason this is not yet supported is that the JSON:API spec does include such schema information.
*Maybe* this is supported by https://www.drupal.org/project/openapi, but I honestly don't know off the top of my head.

Without installing additional modules, you have two clear choices:

  1. Hardcode the allowed values in your client (I know, this sucks).
  2. Read the allowed values from the Field(Storage)Config config entity, which you can read via JSON:API. This is similar to what https://github.com/jsdrupal/drupal-admin-ui does. And actually, perhaps you can just build on top of that project and/or reuse key parts of it? :)

P.S.: please update to the 2.x branch of JSON:API as soon as you can, especially when this is a new project. The 1.x branch is minimally maintained, the 2.x branch is actively being developed and will go into Drupal core.

Wim Leers’s picture

Status: Active » Fixed

Haven't heard back in nearly 3 weeks, so I'm going to assume this helped :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.