Droach5875216
Droach5875216

Looking for a way or some assistance in creating a class/model for extracting and rendering an array of pages from within your pages directory (CMS pages) for use as a nav, or menu. Not looking to use a plugin or pre-made solution for this. Has anyone been able to develop something to this extent?

ndcisiv
ndcisiv

In your class include

use Cms\Classes\Theme;
use Cms\Classes\Page;

Then to get an object of all the pages within the current them, do something like:

$currentTheme = Theme::getEditTheme();
$allPages = Page::listInTheme($currentTheme, true);

Now you can loop through and do whatever you want with it.

foreach ($allPages as $pg) {
    echo $pg->title . ' : ' . $pg->url;
}
Droach5875216
Droach5875216

Honestly, if I could hug you, I would. Cheers man. Great help.

ndcisiv
ndcisiv

Glad to help.

joeRob246811472
joeRob246811472

Hey, just in case anybody is trying to use this for the Static Pages plugin instead of for default pages, just use the Rainlab\Pages\Classes\Page class instead of Cms\Classes\Page.

If you're feeling frisky, you could even do both. For example, I did that below using the built-in lists function.

// get default pages
$pages = \Cms\Classes\Page::sortBy('baseFileName')->lists('baseFileName', 'title');

// also add static pages if the Pages plugin is installed.
if(class_exists('\\Rainlab\\Pages\\Classes\\Page'))
{
  $pages = $pages + \Rainlab\Pages\Classes\Page::sortBy('baseFileName')
    ->lists('baseFileName', 'title');
}

// append default value.
if(count($pages) != 0)
{
  $pages = array('none' => 'Use URL Instead') + $pages;
}

Now $pages holds a lovely associative array with the filename as the key and the title as the value. You can use the |page and |staticPage twig filters on the filename to grab the page url too. Pretty handy.

Last updated

alex11628
alex11628

Since this thread has given me the necessary clues to achieve my goal, I figured I'd share what I have as well. The following function provides a list of options for a Static Pages-style dropdown (preserves hierarchy):

public function getPageOptions() {
    $theme = Theme::getEditTheme();
    $pageList = new PageList($theme);
    $pageTree = $pageList->getPageTree(true);

    $iterator = function($pages, $level=1) use (&$iterator, &$flat_tree) {
        foreach ($pages as $pageInfo) {
            $pageName = $pageInfo->page->getViewBag()->property('title');
            $fileName = $pageInfo->page->getBaseFileName();

            $offset = '';
            for ($i = 0; $i < $level; $i++) {
                $offset .= '&nbsp;&nbsp;&nbsp;';
            }
            $flat_tree[$fileName] = $offset.$pageName;

            if ($pageInfo->subpages) {
                $flat_tree += $iterator($pageInfo->subpages, $level+1);
            }
        }
        return $flat_tree;
    };

    $result = $iterator($pageTree);
    return $result;
}

Last updated

Incremental
Incremental

Hi, very interesting posts, but how would you do to keep a JSON $pageTree, but only with minimal data, ie : page->title and page->baseFileName, keeping the multilevel indentation ?

▼ 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:}

1-7 of 7