Populate the options of a dropdown of a snippet programmatically

Hi,

Is there a way to populate the options of a dropdown of a snippet in October CMS via the php code section of the htm file?

Please see the simplified example below (that does not work), to get an idea of what I want to accomplish.

[viewBag]
snippetCode = "overview"
snippetName = "Overview of pages"
snippetDescription = "Show an overview of the children of a specific page"
snippetProperties[parent][title] = "Parent page"
snippetProperties[parent][type] = "dropdown"

# A list of static pages should be loaded here
snippetProperties[parent][options] = ""
==
function onStart()
{
    $this['parent'] = RainLab\Pages\Classes\Page::load(\Cms\Classes\Theme::getEditTheme(), $this['parent'].'.htm');
    $this['children']  = $this['parent']->getChildren();
}
==

{% for child in children %}
    {{ child.title }}
{% endfor %}

Thanks.

Hey @chocolata

This should work

snippetProperties[parent][options] = "Path\To\Php\Class::staticMethod"

Hi Sam,

Thanks for your answer.

If I do that I get the error:

"Invalid drop-down option key: Chocotools\Classes\Pages. Option keys can contain only digits, Latin letters and characters _ and -"

I tried:

snippetProperties[parent][title] = "Parent page"
snippetProperties[parent][type] = "dropdown"
snippetProperties[parent][options] = "\Chocotools\Classes\Pages::getPages"

With and without the \ prefix. With and without quotes.

My PHP class is inside the plugins folder: /plugins/chocolata/chocotools/classes/pages.php

The class looks like this:

<?php namespace Chocolata\Chocotools\Classes;
class Pages
{
    public static function getPages()
    {

        return ['a'=>'test'];
    }
}

Do you have an Idea what I might be doing wrong?

snippetProperties[parent][options] = "\Chocotools\Classes\Pages::getPages"

The namespace seems to be Chocolata\Chocotools\Classes

Could it be the issue?

Wow, how could I not have seen that. :disappointed_relieved:
Thanks for pointing this out.

This works perfectly. Thank you very much. This gives so much more flexibility.