如何使用获取页面树在 OctoberCMS 中获取静态页面下拉列表?
Posted
技术标签:
【中文标题】如何使用获取页面树在 OctoberCMS 中获取静态页面下拉列表?【英文标题】:How to get static page dropdown in OctoberCMS with get page tree? 【发布时间】:2019-04-27 20:37:35 【问题描述】:我是根据我在Static page dropdown within static page in OctoberCMS 的评论中提出的问题来打开这个的。
我在添加 $pageList->getPageTree(true) 时遇到问题。子页面未显示。我当前的代码是从 $pageObject->subpages 重复 foreach,这不是一个好习惯。
下面是我的代码示例:
插件.php
<?php namespace MyPlugin\CustomPlugin
use System\Classes\PluginBase;
public function boot()
\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->getPageTree(true) as $name => $pageObject)
$pages[$pageObject->page->url] = $pageObject->page->title;
if ($pageObject->subpages)
foreach ($pageObject->subpages as $name => $pageObject)
$pages[$pageObject->page->url] = ' ' . $pageObject->page->title;
if ($pageObject->subpages)
foreach ($pageObject->subpages as $name => $pageObject)
$pages[$pageObject->page->url] = ' ' . $pageObject->page->title;
return $pages;
);
);
如果有人可以提供帮助,不胜感激。
【问题讨论】:
【参考方案1】:也许这会对你有所帮助。
function onStart()
$theme = \Cms\Classes\Theme::getEditTheme();
$pageList = new \RainLab\Pages\Classes\PageList($theme);
$treePageList = $pageList->getPageTree(true);
$pages = [];
$this->getRecursivePage($pages, $treePageList);
dd($pages);
function getRecursivePage(&$pages, $subpages, $level = 0)
$level++;
foreach($subpages as $pageArr)
$pages[$pageArr->page->url] =
str_repeat('-',$level) . ' ' . $pageArr->page->title;
if(count($pageArr->subpages) > 0)
$this->getRecursivePage($pages, $pageArr->subpages, $level);
输出
array:9 [▼
"/content" => "- Content"
"/content/pages" => "-- Static Pages"
"/content/content" => "-- Content"
"/content/models" => "-- Models"
"/content/urls" => "-- URLs"
"/content/urls/tesets" => "--- tesets"
"/test-sp" => "- test-sp"
"/test-sp/oks" => "-- oks"
"/test" => "- test"
]
在这里您可以将-
替换为&nbsp;
或删除该部分[ I added because you had that in code so, may be useful to you. ]
为您的代码
public function boot()
\RainLab\Pages\Classes\Page::extend(function($model)
$model->addDynamicMethod('getPageOptions', function()
$theme = \Cms\Classes\Theme::getEditTheme();
$pageList = new \RainLab\Pages\Classes\PageList($theme);
$treePageList = $pageList->getPageTree(true);
$pages = [];
$this->getRecursivePage($pages, $treePageList);
return $pages;
);
);
public function getRecursivePage(&$pages, $subpages, $level = 0)
$level++;
foreach($subpages as $pageArr)
$pages[$pageArr->page->url] =
str_repeat('-',$level) . ' ' . $pageArr->page->title;
if(count($pageArr->subpages) > 0)
$this->getRecursivePage($pages, $pageArr->subpages, $level);
如有疑问请评论。
【讨论】:
嗨,代码似乎在子页面后不起作用。 imgur.com/a/0Tazzmm子页面循环后所有父页面都变成子页面 hmm,更新了getRecursivePage()
方法请使用新方法,现在应该可以了。以上是关于如何使用获取页面树在 OctoberCMS 中获取静态页面下拉列表?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 OctoberCMS 中使用控制器的默认主题和变量呈现 cms 页面?