Search code examples
phpgrav

Define data-options@ from plugin config file


I made a simple Grav plugin to add some user information! I want to make new blueprints on template.html.twig

This is a plugin config yaml file:

enabled: false
authors:
  -
    name: Author 1
    desc: Description 1
  -
    name: Author 2
    desc: Description 2
    custom_file:

This is a blueprints:

header.author:
   type: select
   label: Author
   classes: fancy
   data-options@: '\Grav\Plugin\AuthorsPlugin::getAuthors'

And I have this in plugin php file:

public static function getAuthors() {

    $author_name = $this->grav['config']->get('plugins.authors.name');

}

I get Error: Using $this when not in object context

Any solutions for this? Thanks!


Solution

  • The problem is that since your function is static, your class hasn't been initialized (no $this or $this->grav which gets set in the constructor when creating an instance of your class).

    Without seeing the entire class, this will hopefully be enough to lead you in the right direction...

    Import the Grav class at the top of your php file if isn't already,

    use Grav\Common\Grav;
    

    Then modify your function to call Grav::instance() rather than $this->grav:

    $author_name = Grav::instance()['config']->get('plugins.authors.name');
    

    This instance() function creates the Grav instance you need to get the config.