Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

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

plugins

Solved by Oliver Scholz View solution

Started by Oliver Scholz 1 year ago · 2 replies · 385 views
1 year ago

Thank’s to the wonderful explanation at https://learn.getgrav.org/17/cli-console/grav-cli-plugin#how-to-use-the-cli-for-plugins, 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

PHP
        $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

PHP
        $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?

1 year ago

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
1 year ago Solution

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:

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

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

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

Suggested topics

Topic Participants Replies Views Activity
General · by jeremycherfas, 4 days ago
3 92 8 minutes ago
General · by Old Man Umby, 2 days ago
1 60 2 days ago
General · by Sebastian, 4 days ago
1 81 4 days ago
General · by Andy Miller, 2 weeks ago
0 216 2 weeks ago
General · by ready-4-IT, 6 months ago
1 716 3 weeks ago