Populate select field with function call (data-*@)

I’m trying to populate a select field in a blueprint with entries from a list field in a plugin configuration.

Consider this excerpt of blueprint.yaml of plugin Foo:

fields:
    bar:
        type: list
        fields:
            .id:
                type: text
                label: "ID"
            .value:
                type: text
                label: "Value"

Now, in a page blueprint, I’d like to have a select field that lists all bar items via their id. I don’t think this is possible with config-*@ syntax, but reading through the documentation on blueprint function calls (data-*@) makes me believe it should be possible this way. However, from the documentation, I don’t understand how to call upon a function from plugin Foo. Here is what I tried:

header.baz:
    type: select
    label: "Baz"
    data-options@: '\Grav\Plugin\Foo::myStaticFunc'

Where the foo.php has the appropriate function (currently just returning dummy data):

public static function myStaticFunc()
{
    // Obviously, this should actually fetch the configuration
    // and then get and return all `id` values of items in `bar`
    return ["some", "values", "please"];
}

I thought this might work, as the documentation has this example: '\Grav\Plugin\Admin::route'. However, the above did not yield any values in the select field. Thinking that I might return the wrong type, I also tried the same with an associative array, to no avail. I’m thinking the error might be in how I call the function, so I also tried the following without success (I’m just guessing at this point):

  • '\Grav\Plugin\Foo\Foo::myStaticFunc'
  • '\Grav\Plugins\Foo::myStaticFunc'
  • '\Grav\Plugins\Foo\Foo::myStaticFunc'
  • '\Grav\Common\Plugin\Foo::myStaticFunc'
  • '\Grav\Common\Plugin\Foo\Foo::myStaticFunc'

Hence, I hope you can help me to figure out…

  • What is the correct syntax and is that documented somewhere?
  • What data structure would a function need to return for data-options@, an array?

@domsson, You may have named the plugin ‘Foo’, but the php class will be named ‘FooPlugin’. In the blueprint, you should be referencing the name of the class, not the name of the plugin.

So try the following:

data-options@: '\Grav\Plugin\FooPlugin::myStaticFunc'
1 Like

Thank you, @pamtbaau - that did the trick. To summarize, here is how it worked:

Blueprint:

data-options@: '\Grav\Plugin\FooPlugin::myStaticFunc'

foo.php:

public static function myStaticFunc()
{
    return ["some" => "Some", "values" => "Values", "please" => "Please"];
}