OctoberCMS中静态页面内的静态页面下拉列表 [英] Static page dropdown within static page in OctoberCMS

查看:99
本文介绍了OctoberCMS中静态页面内的静态页面下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有幻灯片的模板.每张幻灯片包括:

I have a template with a slideshow in it. Each slide consists of:

  • 图片
  • 标题
  • 一段文字
  • 按钮

对于前三个项目,我在模板中设置了以下内容:

For the first 3 items, I have set up the following in my template:

{repeater name="slideshow" prompt="Add another slide" tab="Slideshow"}
  {variable name="image" label="Image" type="mediafinder" mode="image"}{/variable}
  {variable name="caption" type="text" label="Caption"}Caption{/variable}
  {variable name="body" type="textarea" label="Body"}Body{/variable}
{/repeater}

但是,我对用于按钮的字段类型有些困惑,该字段类型需要链接到站点中的静态页面.

However, I'm a little stuck about what field type to use for the button, which needs to link to a static page within the site.

我不想使用用户可以在其中输入URL的文本框,因为这对于用户来说太难理解了.我宁愿有一个下拉菜单,其工作方式与静态菜单插件相同(即:从类型"中选择静态页面",然后在第二个下拉菜单中获取静态页面列表),但它没有看到明显的方法.

I don't want to use a text box where the user can enter a URL as it will be too difficult for the user to understand. I'd rather have a dropdown menu that works in the same way as the static menu plugin (ie: select "Static Page" from "type" and then get a list of static pages in a 2nd dropdown menu) but it doesn't see obvious how to do this.

推荐答案

是的,您可以在其中添加带有静态页面列表的下拉列表.

Yes you can add drop-down there with list of static pages.

要在下拉列表中显示页面列表,您需要扩展页面并向其中添加动态方法,因此当转发器添加下拉列表时,它将从该方法获取其值/选项.

to show list of pages in drop-down you need to extend pages and add dynamic method to it so when repeater add drop down it will get its values/options from that method.

您需要在任何插件内的启动方法中添加代码,这将扩展可进一步处理ajax请求的pages类.

You need to add code to boot method inside any of your plugin, it will extend pages class which can further handle ajax request.

    \RainLab\Pages\Classes\Page::extend(function($model) {
        $model->addDynamicMethod('getPageOptions', function() {
            $theme = \Cms\Classes\Theme::getEditTheme();
            $pageList = new \RainLab\Pages\Classes\PageList($theme);
            $pages = [];
            foreach ($pageList->listPages() as $name => $pageObject) {
                $pages[$pageObject->url] = $pageObject->title . ' (' . $pageObject->url . ')';
            }
            return $pages;
        });
    });

在中继器中,您可以添加下拉列表

and inside your repeater you can add dropdown

    {repeater name="slideshow" prompt="Add another slide" tab="Slideshow"}
      {variable name="image" label="Image" type="mediafinder" mode="image"}{/variable}
      {variable name="caption" type="text" label="Caption"}Caption{/variable}
      {variable name="body" type="textarea" label="Body"}Body{/variable}
      {variable name="page" type="dropdown" label="Page"}{/variable}
    {/repeater}

所以您可以看到我们添加了动态方法 getPageOptions ,它由三个部分组成:1: get 2nd:字段名 3rd:选项

so you can see we added dynamic method getPageOptions it consist of three part 1: get 2nd: fieldname 3rd: Options

因此我们的方法名称将为 get + Page + Options => getPageOptions

在该页面中,我们将返回数组作为value =>标签对,您可以根据需要在此处自定义标签对.

within that page we are returning array as value => Label pair you can customize there as per your need.

因此,在创建下拉列表时,它将搜索此方法并将其返回值用作选项.

so when drop-down created it will search this method and use its return value as option.

如果您发现其他任何困难,请发表评论.

if you find any further difficulties please comment.

好,现在我们终于可以添加条件下拉列表了 到目前为止,它还不是可更新的ajax,但是可以,我们可以根据情况对其进行隐藏和显示.

ok so now we can finally add conditional drop-downs its not ajax updatable so far but yes we can hide and show it based on condition.

您可以在布局中添加此标记.

you can add this mark-up in layout.

    {repeater name="slideshow" prompt="Add another slide" tab="Slideshow"}
      {variable name="type" type="dropdown" label="Link Type"}{/variable}
      {variable name="cms-page" type="dropdown" label="Cms Page" trigger="action:show|field:type|condition:value[cms-page]" }{/variable}
      {variable name="static-page" type="dropdown" label="Static Page" trigger="action:show|field:type|condition:value[static-page]"}{/variable}
      {variable name="blog-post" type="dropdown" label="Post" trigger="action:show|field:type|condition:value[blog-post]"}{/variable}
    {/repeater}

然后在插件内部,您需要添加一些其他方法.

then inside plugin you need to add some additional methods.

您的plugin.php的引导方法看起来像这样

public function boot()
{

    $pluginSelf = $this;
    \RainLab\Pages\Classes\Page::extend(function($model) {
            $model->addDynamicMethod('getTypeOptions', function() {

            return [
                '' => 'Select Type',
                'cms-page' => 'CMS page',
                'static-page' => 'Static Page',
                'blog-post' => 'Blog post'
            ];

        });
    });

    \RainLab\Pages\Classes\Page::extend(function($model) use ($pluginSelf) {
        $model->addDynamicMethod('getStaticPageOptions', function() use ($pluginSelf) {
            $result =  $pluginSelf::getTypeInfo('static-page');
            return $result['references'];
        });
    });

    \RainLab\Pages\Classes\Page::extend(function($model) use ($pluginSelf) {
        $model->addDynamicMethod('getCmsPageOptions', function() use ($pluginSelf) {
            $result =  $pluginSelf::getTypeInfo('cms-page');
            return $result['references'];
        });
    });

    \RainLab\Pages\Classes\Page::extend(function($model) use ($pluginSelf) {
        $model->addDynamicMethod('getBlogPostOptions', function() use ($pluginSelf) {
            $result = $pluginSelf::getTypeInfo('blog-post'); 
            return $result['references'];
        });
    });

}

一种其他方法,您需要在 plugin.php

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;
}

因此,您可以在第一个下拉菜单中看到"cms-page","static-page"和"Blog post"选项,基于此我们可以显示其他下拉菜单.

as a result you can see in first drop down we can have options 'cms-page' , 'static-page' and 'Blog post', based on that we can show other drop downs.

,在显示或使用结果期间,首先需要检查 type 字段的值,然后根据该字段值进一步选择需要使用的下拉列表值,例如 type = cms-页,则需要查找 cms页字段.

and during showing or using results first you need to check value of type field based on that fields value you can further choose which dropdown's value you need to use for example type=cms-page then you need to look for cms-page field.

注意:不确定是否使用-(破折号)输入变量,因此如果没有输入值,可以在 cmsPage 驼峰式的情况下转换变量,如果不需要确定这些内容

notice : not sure used - (dash) in variables so if value is not in out put you can convert variables in cmsPage camel case if needed not sure about this stuff

您需要添加此更改作为更改核心文件的一项附加更改,我也为此创建了拉取请求,因此在我们需要手动执行此更改的同时,它们还将该更改包含在下一个发行版中. 需要此功能,该功能才能正常工作
请参阅此PR请求: https://github.com/octobercms/library/pull/292个/文件

one additional changes you need to add this change as its require to change core file , I also created pull request for it so they also include that change in next release mean while we need to do it manually. its required for this to work
please refer this PR request : https://github.com/octobercms/library/pull/292/files

这篇关于OctoberCMS中静态页面内的静态页面下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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