New addon SelectRequestOptions

While thinking about helper collections for categories with collection links vs select fields with hard-coded options, I combined them to a new, powerful select field.

With this addon it’s possible to define a (custom) request and to set the entries of the response as options for a select field.

Addon: SelectRequestOptions

You can now create a custom request in /config/bootstrap.php

$app->on('admin.init', function(){
    $this->bind('/custom', function(){
        // sample data
        return [
            ['name' => 'Alice', 'location' => 'Wonderland'],
            ['name' => 'Dorothy', 'location' => 'Oz']
        ];
    });
});

and set the response as options for your select field

{
  "request": "/custom",
  "value":"location",
  "label":"name"
}

Or use the built-in helper functions as requests for your select field like /collections/find or /accounts/find and a few more.

Read the README.md for all possible options and more explanations.

2 Likes

you don’t have to include both. One of them is enough. I would recommend keeping:

$this->helper('admin')->addAssets('selectrequestoptions:assets/components/field-select-request-options.tag');

Ah, I just realized, that I can include the entries view renderer in the .tag file, too. So I moved the content of component.js to the top of field-select-request-options.tag. This is nive. It felt wrong to add two get requests for each custom field addon.

I’ll change that in my other addons, too.

I added a new feature. Now the value of the select field isn’t limited to strings anymore. Just map some fields of your request to get an array of objects in your output.

Example:

field options

{
  "request": "/collections/find",
  "options": {
    "collection": "pages"
  },
  "key": "entries",
  "label": "title",
  "value": {
    "name": "title",
    "description": "content",
    "index": "_id"
  },
  "info":"content"
}

select field
request_options

api output

{
    "fields": {...},
    "entries": [
        {
            "req": [
                {
                    "name": "This is a headline",
                    "description": "Lorem ipsum dolor sit amet, consetetur...",
                    "index": "5bb262be3338620a54000183"
                },
                {
                    "name": "another fancy entry",
                    "description": "nothing happened today",
                    "index": "5bba39513338621bec0002d9"
                }
            ],
            "...": "..."
        }
    ],
    "total": 1
}