How to get static page dropdown in OctoberCMS with

2019-08-28 04:12发布

问题:

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.