How to get static page dropdown in OctoberCMS with get page tree?

≯℡__Kan透↙ 提交于 2019-12-14 03:07:39

问题


I am open this based from my question in my own comment from Static page dropdown within static page in OctoberCMS.

I have problem when adding $pageList->getPageTree(true). The child pages did not display. My current code is repeating foreach from $pageObject->subpages which is not a good practice.

Below here is my code example:

Plugin.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] = '&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
                        if ($pageObject->subpages) {
                            foreach ($pageObject->subpages as $name => $pageObject) {
                                $pages[$pageObject->page->url] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $pageObject->page->title;
                            }
                        }
                    }
                }
            }
            return $pages;
        });
    });
}

Appreciate if anyone could help.


回答1:


may be this will help you.

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

Output

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"
]

here you can just replace - to &nbsp; or just remove that part [ I added because you had that in code so, may be useful to you. ]

For your code

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

If any doubt please comment.



来源:https://stackoverflow.com/questions/53477068/how-to-get-static-page-dropdown-in-octobercms-with-get-page-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!