从插件配置文件定义数据options@



我做了一个简单的Grav插件来添加一些用户信息! 我想在模板.html.twig上制作新的蓝图

这是一个插件配置 yaml 文件:

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

这是一个蓝图:

header.author:
type: select
label: Author
classes: fancy
data-options@: 'GravPluginAuthorsPlugin::getAuthors'

我在插件 php 文件中有这个:

public static function getAuthors() {
$author_name = $this->grav['config']->get('plugins.authors.name');
}

我收到错误: 不在对象上下文中使用$this

对此有什么解决方案吗?谢谢!

问题是因为你的函数是static的,你的类还没有被初始化(没有$this$this->grav在创建类的实例时在构造函数中设置)。

在没有看到整个班级的情况下,希望这足以引导您朝着正确的方向前进......

导入 php 文件顶部的 Grav 类(如果还没有),

use GravCommonGrav;

然后修改函数以调用Grav::instance()而不是$this->grav

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

instance()函数创建获取配置所需的 Grav 实例。

最新更新