# New addon SelectRequestOptions

**URL:** <https://discourse.getcockpit.com/t/new-addon-selectrequestoptions/331>\
**Category:** Addons\
**Created:** [October 13, 2018, 10:11pm UTC](https://discourse.getcockpit.com/t/new-addon-selectrequestoptions/331 "2018-10-13T22:11:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 13, 2018, 10:11pm UTC](https://discourse.getcockpit.com/t/new-addon-selectrequestoptions/331/1 "2018-10-13T22:11:07Z")

</div>

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](https://github.com/raffaelj/cockpit_SelectRequestOptions)

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

```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

```json
{
  "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](https://github.com/raffaelj/cockpit_SelectRequestOptions/blob/master/README.md) for all possible options and more explanations.

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [October 13, 2018, 10:50pm UTC](https://discourse.getcockpit.com/t/new-addon-selectrequestoptions/331/2 "2018-10-13T22:50:48Z")

</div>

> <https://github.com/raffaelj/cockpit_SelectRequestOptions/blob/0bd4e317472d920093bcd66bad2adba9704f5b7c/SelectRequestOptions/admin.php#L5-L6>

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

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

```

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 14, 2018, 8:19am UTC](https://discourse.getcockpit.com/t/new-addon-selectrequestoptions/331/3 "2018-10-14T08:19:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [October 19, 2018, 12:16pm UTC](https://discourse.getcockpit.com/t/new-addon-selectrequestoptions/331/4 "2018-10-19T12:16:10Z")

</div>

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**

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

```

**select field**  
 ![request_options](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/1d2d1c635fc2378665e5c4ad845fa7fe8511380e.png)

**api output**

```json
{
    "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
}

```
