是否可以从Grav CMS蓝图格式的另一页访问标头

问题描述

我正在使用自定义页面创建表单来创建模板。在页面B 上,我目前有一个文本框,用户可以在其中输入机构的名称,然后在下拉菜单中选择机构的徽标。

PAGE B:

.logo:
   type: filepicker
   folder: 'user/pages/01.home/04._affiliates/'
   label: Select a logo for the institution
   preview_images: true
   accept:
     - .svg

这些徽标在标题抬头的另一页上。media_order

但是,在 A页上,我具有以下格式的自定义字段

A页:

custom fields format

理想情况下,我想在页面B 上使用一个下拉菜单用户可以在其中从header.associations页面上的user/pages/01.home/04._affiliates/页面A ),然后就可以访问 B页的树枝模板中的名称和SVG徽标。

SelectUnique提供了一个下拉框,但是几乎没有文档,因此我不知道是否有可能访问此类数据。这有可能吗?

解决方法

我不知道这是否是做事的“正确”方法。但是,这是我想出的解决方案:

我使用了一个具有ListInstitutes函数的名为list()的自定义插件。这是代码:

编辑:更新了代码以反映@passerby的评论

<?php
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;


class ListInstitutes extends Plugin
{
    
    public static function list()
    {
        $out_array = array();

        //get static access to Grav page instance 
        $page = Grav::instance()['page'];
    
        //get the affiliates page
        $affiliates = $page->find("/home/_affiliates");

        //get the uri prefix for associated media 
        $media_uri = "/" . str_replace("://","/",$affiliates->getMediaUri()) . "/";
    
        //get the headers for the affiliates page
        $header = $affiliates->header();
    
        //get the associations from the headers 
        $associations = $header->associations;
    
        foreach($associations as $association){
            $out_array[$media_uri . $association["logo"]."||".$association["institution"]] = $association["institution"];
        }  
        return $out_array;
    }
}

然后我在蓝图中创建了该字段:

header.affil:
    type: select
    label: Institute
    size: medium
    classes: fancy
    data-options@: '\Grav\Plugin\ListInstitutes::list'
    default: ''
    options:
       '': 'Select'

这样做,我能够从另一页的标题中检索徽标,然后在新页面中填充下拉菜单。如果以与filepicker相同的方式来支持它,那将是很好的,但这确实可行。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...