For building a vue menu in October, I have a plugin that I want to extract the October pages structure in a JSON data keeping the pages and subpages indentation.
Based on this post : How to get static page dropdown in OctoberCMS with get page tree?
I used the following 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);
}
}
}
but the returned $treePageList is too rich for that purpose and the $pages flattens the indentation.
How could I manipulate the returned JSON structure to simplify it, with only page->url and page->title and keeping the pages and subpages indentation ?
Thanks for help
EDIT :
This code with the $level produces :
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"
]
but I'd like to have a JSON data with levels like (not raw data visualization) :
▼ 0
page {title: , url:}
subpages []
▼ 1
page {title: , url:}
subpages
▼ 0 {title: , url:}
▼ 1 {title: , url:}
▼ 2 {title: , url:}
▼ 3 {title: , url:}
▼ 4 {title: , url:}
▼ 5 {title: , url:}
▼ 6 {title: , url:}
▼ 7 {title: , url:}
▼ 8 {title: , url:}
▼ 2
page {title: , url:}
subpages
▼ 0 {title: , url:}
▼ 1 {title: , url:}
▼ 2 {title: , url:}