Plugin cli: get the active theme's config and directory from php

Thank’s to the wonderful explanation at Plugin Command | Grav Documentation, I’m currently writing a cli command to use as bin/plugin <my plugin> <my command>. Unfortunately, the docs, naturally, don’t cover everything and my PHP skills are so, so.

In my Plugin cli I need to access the currently active theme, its config and its directory (though the latter might not be absolutely necessary).

In a “live” instance, for example from <theme-name>.php, I could get all that with something like

        $grav = Grav::instance(); // or maybe $this->grav
        $themecfg = $grav['config']['theme'];
        $themedir = $grav['locator']->findResource('theme://', false);

But if I use this in the plugin cli code, the former is NULL and ‘theme://’ is invalid.

I can get the configs of all installed themes and the theme directory, though, with

        $grav = Grav::instance();
        $all_theme_cfgs = $grav['config']['themes']; // theme_s_, plural
        $themes_dir = $grav['locator']->findResource('themes://', false);

So, I’m assuming, this is because the plugin cli instance didn’t initialize theme related stuff. So, how do I do this?

I think you’d have to call the getter as Grav::instance()['config']->get('theme'); because of how it is instantiated. Depending on the data your working with, there’s a lot more that you may want to instantiate before processing, you can see an example of what and how in the Static Generator SSECollection’s setup(). Pages, for instance, do not exist until evaluated, and so that plugin has to bootstrap() them. The same has generally held for most parts of Grav: The instance itself is just the beginning, a lot happens in the lifecycle further on.

1 Like

Thank you! Grav::instance()['config']->get('theme') alone doesn’t do anything for me, but the code of your plugin contains the solution. A call to →init() is all that’s needed for my purpose:

    $grav['config']->init();
    $grav['themes']->init();

    var_dump($grav['config']['theme']);

    $var = $grav['locator']->findResource('theme://', false);
    var_dump($var);