Generate select options in a form

Hello.
I am trying to get a dynamic list of subpages in a form <select> field.

Here’s an extract of what I’m trying to write in the frontmatter:

content:
    items: @self.children
form:
    name: select-a-page
    fields:
        -
            name: page
            label: 'What page?'
            type: select
            options:
                '{% for p in page.collection %},{{ p.slug }}:{{ p.title }},{% endfor %}'
            validate:
                required: true

But it gets an Invalid frontmattererror when saving. How can I do this?

Thanks!

Nevermind for the Invalid, it was something else. It does save, but the selector is empty.

There’s no way to do what you are trying to do. The problem is that even if you enable Twig processing in frontmatter: pages: frontmatter: process_twig: true (in system.yaml), when that runs the collection defined by the content items has not been processed.

The way you should do dynamic options is to call a static function.

You can put this function in a plugin or even in your theme. You just need to reference it adaquately.

Hi, thank you a lot for your answer, I will look into this.
What would the function look like? I understand how to call it, but not exactly how to write it.

It just needs to return a PHP array in a name/value format. e.g.

$options = [
  'option-a' => 'Option A',
  'option-b' => 'Option B'
];

return $options;
---