静态页面插件-将所有页面url作为自定义页面字段的列表 [英] Static page plugin - List with all pages url as a custom page field

查看:92
本文介绍了静态页面插件-将所有页面url作为自定义页面字段的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用静态页面插件.我需要一种从自定义字段中获取octobercms所有页面的方法.我不在乎如何完成此操作(官方页面网址选择器-下拉列表...),我只希望对用户友好,因此可以从客户端使用.

I'm using static page plugin. I need a way to get from a custom field all the pages of the octobercms. I don't care how this will be done (official page url selector - dropdown list ...), I want only to be user friendly so can be used from a client.

有一种解决方法,但是它仅适用于中继器.如果我将其与其他字段{variable name="page" label="Page" type="dropdown"}{/variable}一起使用,则会出现错误:

There is a workaround but it works only with repeater. If I use it with one other field {variable name="page" label="Page" type="dropdown"}{/variable} I got an error:

get_class() expects parameter 1 to be object, array given

推荐答案

哇!这个问题确实使我的大脑更加紧张.

Wow! this question really tighten my brain's nuts and bolts,

感谢您提出的问题,我真的很乐意解决.

Thanks for the Question, I really enjoyed it to solve.

就像我们在转发器十进制字段之外使用普通变量一样,十月字段解析会将其视为viewBag[page]的名称,并且不适用于fieldParserfieldOptions getter方法. (其技术性不高,所以我想跳过这一点,跳到真正的解决方案)

As if we use normal variable outside of the repeater October field parse will treat it as name as viewBag[page] and it is not working with fieldParser and fieldOptions getter methods. (its little technical so I would like to skip this and jump to real solution)

最后我怎么发现并没有发现一点扩展/功能(十月Cms的广泛性质)并使用它

any how at last I found little hack/kind of extension/functionality (extensive nature of October Cms) and use it

您需要任何插件,以便可以在boot方法内编写扩展代码.

you need any plugin so you can write extension code inside boot method.

public function boot() {
    $alias = \Illuminate\Foundation\AliasLoader::getInstance()
              ->alias('MyPluginOptionAlias','October\\Demo\\plugin');
}

在这里您可以更正alias-namenamespace,我们正在使用此alias (MyPluginOptionAlias)

here you can correct alias-name and namespace we are making our plugin object globally available using this alias (MyPluginOptionAlias)

plugin.php内,我们添加了两个static方法,以便以后可以访问它们,这只是获取page-list并将其作为数组返回的logic.

and inside plugin.php we add two static methods so we can access them later, this will be just logic of getting page-list and return it as an array.

public static function getPageOptions() {

    // different lists // or even you can merge them
    $result =  self::getTypeInfo('cms-page');
    // $result =  self::getTypeInfo('static-page');
    // $result =  self::getTypeInfo('blog-post');
    return $result['references'];
}


public static function getTypeInfo($type)
{
    $result = [];
    $apiResult = \Event::fire('pages.menuitem.getTypeInfo', [$type]);

    if (is_array($apiResult)) {
        foreach ($apiResult as $typeInfo) {
            if (!is_array($typeInfo)) {
                continue;
            }

            foreach ($typeInfo as $name => $value) {
                if ($name == 'cmsPages') {
                    $cmsPages = [];

                    foreach ($value as $page) {
                        $baseName = $page->getBaseFileName();
                        $pos = strrpos($baseName, '/');

                        $dir = $pos !== false ? substr($baseName, 0, $pos).' / ' : null;
                        $cmsPages[$baseName] = strlen($page->title)
                            ? $dir.$page->title
                            : $baseName;
                    }

                    $value = $cmsPages;
                }

                $result[$name] = $value;
            }
        }
    }

    return $result;
}

现在最重要的

{variable name="page" label="Page" type="dropdown" options="MyPluginOptionAlias|getPageOptions" tab="link"}{/variable}

您像这样 MyPluginOptionAlias|getPageOptions

you define options exactly like this MyPluginOptionAlias|getPageOptions

这里的逻辑是现在它将从我们的MyPluginOptionAlias实例的 method: getPageOptions

Logic here is that now it will fetch option list from our MyPluginOptionAlias instance's method: getPageOptions

您从中返回的array都会作为此下拉列表的选项列出.

what ever array you return from it will be listed as options for this drop-down.

如果有任何问题,请发表评论,我们将予以纠正.

please comment if it creates any issue, we will correct it.

这篇关于静态页面插件-将所有页面url作为自定义页面字段的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆