简体   繁体   English

从插件配置文件中定义data-options @

[英]Define data-options@ from plugin config file

I made a simple Grav plugin to add some user information! 我制作了一个简单的Grav插件来添加一些用户信息! I want to make new blueprints on template.html.twig 我想在template.html.twig上制作新的蓝图

This is a plugin config yaml file: 这是一个插件配置yaml文件:

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: 我在插件php文件中有这个:

public static function getAuthors() {

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

}

I get Error: Using $this when not in object context 我得到错误:在不在对象上下文中时使用$ this

Any solutions for this? 对此有何解决方案? Thanks! 谢谢!

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). 问题是,由于你的函数是static ,你的类还没有初始化(在创建类的实例时没有$this$this->grav在构造函数中设置)。

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, 如果尚未导入php文件顶部的Grav类,

use Grav\Common\Grav;

Then modify your function to call Grav::instance() rather than $this->grav : 然后修改你的函数来调用Grav::instance()而不是$this->grav

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

This instance() function creates the Grav instance you need to get the config. instance()函数创建获取配置所需的Grav实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM