Search code examples
octobercmsoctobercms-pluginsoctobercms-backend

Extracting October Static Pages MainMenu items for a Vue.js app


For building a Vue menu in October, I have the following code in a backend plugin. It is working fine to get the Static Pages pages in a JSON data, keeping the pages items indentation :

$theme = \Cms\Classes\Theme::getEditTheme();
$pageList = new \RainLab\Pages\Classes\PageList($theme);
$treePageList = $pageList->getPageTree(true);

Now I would like to extract the October "RainLab Static Pages / Menus / Mainmenu" items, keeping the menu items indentation. (In my backend plugin).

Thanks for any idea about how to get these menu items ?

enter image description here


Solution

  • Thanks Hardik Satasiya, I solved it with the following code :

    if(class_exists('\\Rainlab\\Pages\\Classes\\Menu')) {
        $theme = \Cms\Classes\Theme::getActiveTheme();
        $menus = \RainLab\Pages\Classes\Menu::listInTheme($theme, 'mainmenu');
        $menu_full = json_decode($menus);
    
        return getRecursiveMenu($menus);
    }
    
    function getRecursiveMenu($menus) {
        static $level = 0;
        static $next_level = 0;
        $menuDetails = [];
        foreach($menus as $iMenu) {
            $detail = [];
            if ($level == $next_level) {
                $detail['menu_name'] = ['name' => $iMenu->name];
            } else
                $detail['menu_name'] = ['title' => $iMenu->title, 'url' => $iMenu->reference];
    
            $level++;
            $items = getRecursiveMenu($iMenu->items);
    
            if(count($items) > 0 ) {
                $detail['menu_items'] = $items;
            }
            $menuDetails[] = $detail;
            $next_level++;
        }
        return $menuDetails;
    }
    

    Thanks for your help !