Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

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

plugins

Solved by Oliver Scholz View solution

Started by Oliver Scholz 9 months ago · 2 replies · 53 views
9 months 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?

9 months 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
9 months 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 Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 14 hours ago
1 51 13 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago