Select Field: Dynamic Options from Page Model Method

Is there anybody out there with an example of a page model method that returns valid options for the select field (https://getkirby.com/docs/reference/panel/fields/select#dynamic-options)?

$result = array();
// implement me...
array_push($result, ['value' => 0, 'text' => 'Nothing found']);
return $result;

This implementation yields the following error:

get_class() expects parameter 1 to be object, array given

Thanks!

You need a flat array of key/value pairs:

Example

class AlbumPage extends Page
{
    public function cover()
    {
        return $this->content()->get('cover')->toFile() ?? $this->image();
    }
    public function options() {
        return ['single' => 'Single', 'repeat' => 'Repeat'];
    }
}

In blueprint:

fields:
  selectfield:
    type: select
    options: query
    query: page.options

Your array, however, returns an array of arrays

Array
(
    [0] => Array
        (
            [value] => 0
            [text] => Nothing found
        )

)

Thank you very much. I was looking way to far :upside_down_face:

Although, this looks correctly in the panel, it seems not to work as expected.

The options from you example translate in this JSON payload:

As you can see, textand value is identical.

That was finally the reason why I was experimenting with other constructs such as my proposed array and with Collection or Structure objects.

Hm :thinking: