2

I have a number of sites using the plain vanilla native 'Pages' module in EE where I would like to output a simple 'child pages' list for pages with children (based on the page_uri).

In the Pages module itself pages are displayed with nesting so I'm guessing the data is there somewhere - but how to get at it?

I'm not adverse to writing a plug-in but could do with some guidance on ow the pages module knows what's a child of what?

Update: I think what I'm looking for is an EE2 version of Mark Huots plag-in for the original pages module - http://devot-ee.com/add-ons/pages-nested-menu

Update: Using the EE1.x > EE2.x upgrade instructions I've managed to get a version of this plug-in working for EE2.x. It's still a little rough around the edges but it's better than nothing!

https://github.com/ninefour/pages_nested_menu.pi.ee_addon

1

A plugin is definitely required here. Unfortunately, the Pages module doesn't store data in a hierarchical format, and the logic used in the module control panel is not ideal for use as a plugin (as you need a properly nested array for that). So you're starting from scracth.

But here's something to get you started: all pages are stored in $this->EE->config->item('site_pages'), as an array with site_ids as keys, and then further arrays with entry_ids as keys and page URIs as values. So you'll need to query the titles for each.

Here's some quick starter code:

$pages = $this->EE->config->item('site_pages');
if ($pages === FALSE OR count($pages[$this->EE->config->item('site_id')]['uris']) == 0)
{
    return false;
}

$pages = $pages[$this->EE->config->item('site_id')]['uris'];
natcasesort($pages);

$titles = array();
$entry_ids = array_keys($pages);
$entry_ids = implode(',', $entry_ids);

$titles_q = $this->EE->db->query("SELECT entry_id, title FROM exp_channel_titles WHERE entry_id IN($entry_ids)");
if($titles_q->num_rows() > 0)
{
    foreach($titles_q->result_array() as $entry)
    {
        extract($entry);
        $titles[$entry_id] = $title;
    }
}

print_r($pages);
print_r($titles);

So this gives you two arrays, both with the entry_id as a key, and one with the URI as the value, and another with the Title as the value. From here you'll need to do some sorting, explode the URI values using a forward slash as a delimiter, and build a new array, etc. Lots of array juggling to build this with a proper hierarchy, and in the order you want it.

In the end, you want to end up with something like this (to pass to $this->EE->TMPL->parse_variables):

$pages = array(
    [0] => array(
        'entry_id' => XX,
        'title' => 'About Us',
        'uri' => '/about',
        'children' => array(
            [0] => array(
                'entry_id' => XX,
                'title' => 'Staff',
                'uri' => '/about/staff'
            ),
            [1] => array(
                'entry_id' => XX,
                'title' => 'History',
                'uri' => '/about/history'
            ),
        )
    ),
    [1] => array(
        'entry_id' => XX,
        'title' => 'Location',
        'uri' => '/location',
        'children' => ''
    )
    [2] => array(
        'entry_id' => XX,
        'title' => 'Contact Us',
        'uri' => '/contact',
        'children' => ''    
    )
);
| improve this answer | |
  • Hi Derek - thanks ever so much for the pointer, I'll post back here with any progress! :) – Nathan Pitman Feb 13 '13 at 21:37
  • Ok, well I got some way by following the EE1.x > EE2.x upgrade instructions and it seems to work! However there's one little niggle I can't nail - the plug-in is outputting an addition closing </li>... github.com/ninefour/pages_nested_menu.pi.ee_addon – Nathan Pitman Feb 14 '13 at 1:55
  • Actually I think I fixed it! I also added highlighting for the current page via the addition of an 'active' class on the corresponding <li>. – Nathan Pitman Feb 14 '13 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.