Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

can’t get @data-options function call to work

Solved by Anna View solution

Started by Anna 3 days ago · 7 replies · 81 views
3 days ago

First off, what I would ultimately like to achieve is several optgroups in a frontend form select field, consisting of four parent pages and their respective children as options.

I think I need to write my own function for that (because of the optgroups), but to get me started I copied the example from the docs:

YAML
form:
  fields:
    route:
      type: select
      label: Parent
      classes: fancy
      data-default@: '\Grav\Plugin\Admin::route'
      data-options@: '\Grav\Common\Page\Pages::parentsRawRoutes'
      options:
        '/': '- Root -'

And this already returns nothing, sadly. I tried some other methods I found in the PHP reference for Pages, but the only one that returns anything is \Grav\Common\Page\Pages::types – that returns the types alright. But all or children or routes don’t return anything, and neither can I see any errors or other hints.

I don’t know how to debug this, or more importantly, I don’t know how to create a function in my theme that returns something. I also don’t know if I need to register a custom data provider for that like the docs suggest, so I wanted to try the example first, and that is already tripping me up.

Suggestions and thoughts much appreciated!

2 days ago

I was under the impression that field properties like data-*@ were only called when used in blueprints (Admin) and not in front-end forms. However, upon testing, it appears that, when using Gav 2.0 and registering your own function, the function does get called when used in a front-end form.

I added the following to a plugin (adapt when using a theme):

PHP
    public function onPluginsInitialized(): void
    {
        ...
        Blueprint::addAllowedDynamicCallable('Grav\Plugin\MyPlugin::myDefaultData');
        ...
    }

I used it in my test using the the following form snippet:

YAML
form:
    name: contact

    fields:
        ...
        route:
            type: select
            label: Parent
            classes: fancy
            data-options@: '\Grav\Plugin\MyPlugin::myDefaultData'
        ...

and the following plugin function:

PHP
public static function myDefaultData()
{
    $grav = \Grav\Common\Grav::instance();

    $pages = $grav['pages'];

    // build your own options
    return ['/' => 'myroot', 'home' => '/myhome'];
}

Resulting form:

image.png

2 days ago

I’m sorry. That is such a clear and concise example, and I’m still struggling.

I was definitely missing the registering function in my theme 😆 so I thought that would be it, but I put all the things in my theme and – no output.

Then I copied your code almost exactly (without the dots) into a new plugin, just to see if that would make a difference, and that throws a crikey in Admin with “Class "Grav\Plugin\Blueprint" not found“. 🤨 Plugin php looks like this:

PHP
namespace Grav\Plugin;

use Grav\Common\Plugin;
use Grav\Common\Data;
use RocketTheme\Toolbox\Event\Event;

class CallablePlugin extends Plugin
{
    public static function getSubscribedEvents()
    {
        return [
            'onPluginsInitialized' => ['onPluginsInitialized', 0]
        ];
    }

    public function onPluginsInitialized(): void
    {
        Blueprint::addAllowedDynamicCallable('Grav\Plugin\CallablePlugin::myDefaultData');
    }

    public static function myDefaultData()
    {
        $grav = \Grav\Common\Grav::instance();

        $pages = $grav['pages'];

        // build your own options
        return ['/' => 'myroot', 'home' => '/myhome'];
    }

}

And the theme php looks like this:

PHP
namespace Grav\Theme;

use Grav\Common\Theme;
use Grav\Common\Grav;
use RocketTheme\Toolbox\Event\Event;

class Basis extends Theme
{
    public static function getSubscribedEvents(): array
    {
        return [
            'onThemeInitialized'    => ['onThemeInitialized', 0],
        ];
    }

    public function onThemeInitialized(): void
    {
        Blueprint::addAllowedDynamicCallable('Grav\Theme\Basis::tourRoutes');
    }

    public static function tourRoutes()
    {
    return ['/' => 'myroot', 'home' => '/myhome'];
    }
}

What am I missing here?!

2 days ago

You said:

in a frontend form select field

and

that throws a crikey in Admin

Are you needing the data_options@ in a frontend form (the website), or in a backend blueprint (Admin)?
When used in a blueprint, there is no need to register the function.

Class "Grav\Plugin\Blueprint" not found“

You plugin class is called "CallablePlugin"...

2 days ago Solution

Are you needing the data_options@ in a frontend form (the website), or in a backend blueprint (Admin)?

Frontend. It’s just that the crikey turns up everywhere, naturally.

You plugin class is called "CallablePlugin"...

Okay so, I absolutely consider myself a dabbler in php, I mostly do frontend development. While I theoretically understand the concept of namespaces, in practice they often throw me for a loop. After more dabbling though I realised I just needed to add use Grav\Common\Data\Blueprint;, now the plugin gives me output and all is well!

I would still love to know why it’s not working in the theme’s php. I changed the same things, still no output. Either something fails very silently (nothing in the logs), or it’s not even getting called I guess? No worries if that’s not worth figuring out, I can create a plugin. Just a quest of gaining better understanding.

2 days ago

Turns out that optgroup selects do not work with the data-options@ trick. I tried passing a nested array, but it gets flattened and outputs just two optgroup headings with no options underneath.

I tried this:

PHP
$arrOptions = [
    'Optgroup 1' => [
        '1-1' => 'Group 1 Option 1',
        '1-2' => 'Group 1 Option 2',
        '1-3' => 'Group 1 Option 3',
    ],
    'Optgroup 2' => [
        '2-1' => 'Group 2 Option 1',
        '2-2' => 'Group 2 Option 2',
        '2-3' => 'Group 2 Option 3',
    ],
];
return $arrOptions;

and the output is <optgroup label="1-1"></optgroup><optgroup label="2-1"></optgroup>.

However, this is a new problem if related, so I’m going to make a new post about it…

1 day ago

After more dabbling though I realised I just needed to add use Grav\Common\Data\Blueprint;, now the plugin gives me output and all is well!

Any proper code-editor will tell you what's wrong and where... And can add the use Grav\Common\Data\Blueprint; statement for you.

This is what free VSCode with free plugin intelephense will tell you:

image.png

Turns out that optgroup selects do not work with the data-options@ trick.

That's a bold statement. You only haven't found out how yet...
The generated label="1-1" is giving a hint as of how the array should be structured.

last edited 11/30/26 by pamtbaau
15 hours ago

That's a bold statement.

You are quite right, and I retract it humbly! Thanks for all the help. Will accept your solution in the other thread now.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Anna, 2 days ago
2 51 15 hours ago
Forms & Blueprints · by Anna, 1 week ago
5 185 6 days ago
Forms & Blueprints · by TomW, 1 month ago
3 377 1 month ago
Forms & Blueprints · by Japhy, 2 months ago
2 593 2 months ago
Forms & Blueprints · by FrViPofm, 2 months ago
5 623 2 months ago