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

前端 未结 1 734
名媛妹妹
名媛妹妹 2020-12-22 06:29

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

相关标签:
1条回答
  • 2020-12-22 06:51

    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   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.

    0 讨论(0)
提交回复
热议问题