【问题标题】:Is it possible to access headers from another page in Grav CMS blueprint form是否可以从 Grav CMS 蓝图表单中的另一个页面访问标题
【发布时间】:2020-11-07 10:35:35
【问题描述】:

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

第 B 页:

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

这些徽标在 header.media_order 中上传的另一个页面上

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

第 A 页:

理想情况下,我希望在 Page B 上有一个下拉菜单,用户可以在其中从 user/pages/01.home/04._affiliates/ 页面上的 header.associations 选择机构(Page A)并且可以从那里访问 Page B 的树枝模板中的名称和 SVG 徽标。

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

【问题讨论】:

    标签: forms dropdown blueprint grav


    【解决方案1】:

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

    我用 list() 函数创建了一个名为 ListInstitutes 的自定义插件。代码如下:

    编辑:@passerby 更新代码以反映 cmets

    <?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 相同的方式得到支持,那就太好了,但这是可行的。

    【讨论】:

    • 更“正确”的一点是遵循 Grav 标准的插件创建方式。查看 devtools 插件 (github.com/getgrav/grav-plugin-devtools) 并使用 $ bin/plugin devtools newplugin 创建插件。另外,PHP方法list不应该是static吗?
    • 我想你需要将.affil的值保存到页面的头部,如果是这样,它的字段名应该是header.affil
    • $media_uri 不在任何地方使用。
    • 我更新了上面的代码以反映您的更改。列表是静态的,然后我将其删除以测试其他内容,但它不会影响它。但是,你是对的,它应该是静态的,所以我已经改变了。 $media_uri 应该被使用。是否每个字段都需要包含标题,实际上我发现它只需要在声明的第一个字段上,然后 .affil 在那之后工作(虽然这是不好的做法)?感谢您的反馈意见。我将根据文档使用 devtools 再次创建插件。
    • 是的,您需要\Grav\Common\Page\Page-&gt;header() 来获取/设置整个标头对象(请参阅learn.getgrav.org/16/api#class-gravcommonpagepage)。我认为您可以将答案标记为“解决方案”...
    猜你喜欢
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2021-01-30
    • 1970-01-01
    • 2019-08-04
    • 2017-12-01
    相关资源
    最近更新 更多