I'm doing a custom template for the Concrete5 autonav block and am wondering if there is a way to get the total number of descendants (children, grandchildren, and so on) for each particular nav item? For example, get descendants for the top level navigation to show the total number of pages that are descendants under each:
Tutorials (33)
Freebies (25)
Lesson Plans (10)
The Autonav block provides a flat array of objects (representing each page), and each object has an ID for it's parent, but I can't wrap my head around looping through and building an array for the multilevel navigation.
Does Concrete5 offer a method for this, or do I need to figure out how to build a loop that extracts this information?
I essentially am looking for the functionality of this thread (unfortunately they don't answer the question)
Important!
Don't forget to copy the content from
/concrete/blocks/autonav/
into
/application/blocks/autonav/
(Create the folders blocks
and autonav
if necessary)
and do these changements there. Otherwise they'll be gone on the next system update!
As a $navItem
has the following property:
$navItem->cID : collection id of the page this nav item represents
Add a method to the controller (This is a quickie though):
public function getChildPagesFromID($cID)
{
$db = Database::connection();
$r = $db->query(
"select cID from Pages where cParentID = ? order by cDisplayOrder asc",
array($cID));
$pages = array();
while ($row = $r->fetchRow()) {
$pages[] = Page::getByID($row['cID'], 'ACTIVE');
}
return sizeof($pages);
}
Then you can do this in the view.php
:
// Existing code:
if (count($navItems) > 0) {
echo '<ul class="nav">'; //opens the top-level menu
foreach ($navItems as $ni) {
echo '<li class="' . $ni->classes . '">';
Add this somewhere inside the <li>
Tag:
echo $controller->getChildPagesFromID($ni->cID);
The $ni->cObj->getNumChildren();
as suggested in the forum gets System pages too. I don't think it is what you want.
As for your comment "seems to bring in System pages too" that depends if you decide to show them in your Auto-Nav block: