Using option in a route in a plugin

Hi all,

Does anybody have an idea if it’s possible to use options in routes in a plugin? I keep getting the defaults from the plugin, tried using ready, system.loadPlugins:after but nothing works :man_shrugging:.

Below an example, so I want to be able to overwrite the slugs via the site/config.php. So i keep getting the once defined in the plugin, no matter what I do in site/config.php.

<?php
Kirby::plugin('my/plugin', [
    'options' => [
        'slug' => [
            'nl' => 'nieuws',
            'en' => 'news',
            'fr' => 'actualites',
        ]
    ],
    'routes' => function($kirby) {
           $newsUri = $kirby->option("my.plugin.slug.{$kirby->language()}", "news");
           
           return [
               [
                   'pattern' => "{$newsUri}/(:any)",
                   'action' => function($any) {
                       if(!empty($any))
                       {
                           $page = page("dbnews/{$any}");
    
                           if(empty($page))
                           {
                               go('error');
                           }
    
                           return $page->render();
                       }
    
                       go('error');
                   },
                   'method' => 'GET',
               ],
           ];
       }, 
]);

Any ideas?

That returns a language object, try $kirby->language()->code() instead.

You’re right but that’s not the problem though.

If i parse the $newsUri in the route callback in the plugin I always get the option data that is set by the plugin, not the one I set with my config.

A workaround could be to define your options like this:

'options' => [
        'slug' => [
            'nl' => 'nieuws',
            'en' => option('my.plugin.slug.en', 'news'),
            'fr' => 'actualites',
        ]
    ],

1 Like

this works,… o … m … g, you can’t imagine how many different things I already tried :man_facepalming:
As always, you rock @texnixe :sweat_smile:

Cheers!

1 Like