Using the JSON API to populate checkboxes

Heya

I am sort of struggling with the JSON API for checkboxes: Checkboxes | Kirby CMS

Not sure if I either did not understand it correctly, or if the documentation is missing something

I am using this route to create a JSON:
(get all tags present on the site and then populate an array with the count of the usage of the tags)

    [
      'pattern' => 'my-api/tags.json',
      'action'  => function () {
        $tags = [];
        foreach (site()->index()->pluck('tags', ',', true) as $k => $tag) {
          $tags['Tags'][] = [ 'value' =>  $tag, 'text' => $tag . ' (' . site()->index()->filterBy('tags', $tag)->count() . ')' ];
        };
        return json_encode($tags);
      }
    ],

JSON:

{
  "Tags": [
    {
      "value": "cats",
      "text": "cat (21)"
    },
    {
      "value": "dogs",
      "text": "dogs (0)"
    },
(...)

this JSON is read by this blueprint:

categories:
  type: checkboxes
  options: api
  api:
    url: "https://www.example.com/my-api/tags.json"
    fetch: Tags
    text: "{{ item.text }}"
    value: "{{ item.value }}"

This works.

But afaik this should be easier.
These things don’t seem to work for me:

  • using url: "{{ site.url }}/my-api/tags.json" the checkbox blueprint it empty.
    if I use the full URL it works
  • instead of the “full” blueprint, trying to just use api: https://www.example.com/my-api/tags.json does not work either (this is from the docs under #06).
    It seems that the displayed text is the value, not the text item in the json.
    The docs refer to the “manual option setting”, which I can’t seem to find in the docs?
    Again, using {{ site.url }} does not work, either.
    (nb: I change the JSON to be “flat”; to not use “Tags” as the first element)

What am I missing?!
Any help is much appreciated, as I seem to get nowhere for the last hours :confused:

Oh no, impossible.

"{{ site.url }}/my-api/tags.json" works for me, though (tested with the code above in a Kirby 3.3.3 Starterkit)

On a side note, json_encoding() the $tags array is superfluous.

damn! always those quick replies, nice! Thank you Sonja!

hm. then it might me my sever / kirby version.
I’ll check that regarding {{ site.url }}

Does “my” code work for you just using the api and url directly? Without the use of text and value?

I’ll check…

The api itself works, but I don’t get the text/value pairs but only the value as was to be expected.

I’m not sure I understand: it is expected to only use the value?

Yep, that’s the default if you don’t use the long form…

forgive my ignorance, but this is not clear for me from reading:

By default, the API setting expects that the JSON endpoint returns an option array as shown above in the manual option setting.

That line refers to defining the options manually with key/value pairs: https://getkirby.com/docs/reference/panel/fields/checkboxes#example

Here it says that to get different value and text options, you need to use what I call the long form of the query: https://getkirby.com/docs/reference/panel/fields/checkboxes#dynamic-options__custom-text-and-value

1 Like

aaaah!! Now it makes sense :slight_smile:
Thank you!
This now works:

    [
      'pattern' => 'my-api/tags.json',
      'action'  => function () {
        $tags = [];
        foreach (site()->index()->pluck('tags', ',', true) as $k => $tag) {
           $tags[$tag] = $tag . ' (' . site()->index()->filterBy('tags', $tag, ',')->count() . ')';
        };
        return json_encode($tags);
      }
    ],
options: api
api: "https://example.com/my-api/tags.json"