Muut
May 4, 2016, 9:33pm
1
Hey there,
I’m trying to fill a select field with dynamic options, like pages from a certain subfolder, but it does not seem to work in frontend forms.
form:
name: contact
fields:
- name: Name
type: text
label: "Your Name"
- name: Recipient
type: select
label: "send to"
'@data-options': '\Grav\Theme\MyOwnTheme::getRecipients'
So is there a way to populate a select with dynamic options? Or is it possible to create a complete custom Form-Field-Type?
Muut
May 5, 2016, 8:08am
2
What version of Grav are you using? If I recall correctly, Twig variables will be available in FrontMatter in 1.1 (which is in Beta currently).
Muut
May 5, 2016, 11:37am
3
Ok, sounds good! But I’m using version 1.0.10 and a Beta is not an option for this project. Think I’m going to write a for this if there is no other way.
Muut
May 5, 2016, 3:07pm
4
You can call a static method from the YAML definition of a form even in Grav 1.0.
See this example to get the available Timezones in the system/blueprints/system.yaml
- https://github.com/getgrav/grav/blob/develop/system/blueprints/config/system.yaml#L59
And the corresponding static function it calls in Utils: https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Utils.php#L363-L387
Muut
May 5, 2016, 3:27pm
5
That’s exactly what I have tried, but it does not seem to work in the frontend with the form plugin. I even tried to use that example and added the following to my form frontmatter in the form.md file
form:
name: 'MyForm'
fields:
...
- name: timezone
type: select
label: PLUGIN_ADMIN.TIMEZONE
size: medium
classes: fancy
help: PLUGIN_ADMIN.TIMEZONE_HELP
data-options@: '\Grav\Common\Utils::timezones'
default: ''
options:
'': 'Default (Server Timezone)'
...
The result is, that the select input is shown, but only has one option: 'Default (Server Timezone)'
Also the label is not shown, and i tried to use the key ‘data-options@’, as in your given example, as well as ‘@data-options ’ li in my systems system.yaml
So I guess, this does not work in frontend forms. Can anybody reproduce that behavior?
Muut
August 23, 2016, 5:14pm
6
I have Grav 1.1.3 and can’t get a select field to dynamically populate from my theme.
—yaml
data-options@: ‘\Grav\Theme\MyGravTheme::getElementSizeOptions’
---php
public static function getElementSizeOptions() {
$sizes = [
'full' => 'Full Width',
'eighty' => '80%',
'sixty' => '60%',
'half' => '50%',
'forty' => '40%'
];
return $sizes;
}
---
Muut
August 24, 2016, 11:27pm
7
The frontend theme class is probably not loaded in the admin (it is in the frontend). Can you put this in a plugin instead?
Muut
September 26, 2016, 2:23pm
8
I have the same problem as timbo in a front-end form, can’t populate even from
data-options@: ‘\Grav\Common\Utils::timezones’
Also tried from my own plugin, doesn't populate either
data-options@: ‘\Grav\Plugin\MyPlugin::myutil’
Just updated to last version of Grav. Is there another way of doing this?
Muut
September 26, 2016, 3:29pm
9
I finally solved this by writing a simple Plugin, so build the form “by-hand” and so populate the select boxes direct in the twig template.