Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Solution for displaying a filtered count of grandchild pages?


Go to End


1655 Views

Avatar
pinkp

Community Member, 182 Posts

26 November 2015 at 11:27pm

I have a page (A) with children (B's) each of these children has children (C's).
On page A I want to display a list of B's with a count next to each which counts the C's belonging to B. Example here: http://bit.ly/1T0TylS

I can do this but I want the count to filter / exclude some of the C pages depending on if a boolean is true on the page.

I did start this question here: http://stackoverflow.com/questions/32099701/silverstripe-3-filtering-filtering-out-dataobjects-in-a-function
And the answer so far was:

public function getGrandChildren() {
$ids = Page::get()->filter(array('ParentID' => $this->ID))->getIDList();
$grandChildren = Page::get()
->filter(array(
'ParentID' => $ids
))
->exclude(array('DealerOnly' => true));

return $grandChildren;
}

but i think this method doesnt work as its not a data object I'm excluding but a page.. It just counts all the pages still.

I'm using a function to get the grand children C's rather than looping as that would not collate them correctly.

This is my grandChildren function:

public function getGrandChildren() {
$ids = Page::get()->filter(array('ParentID' => $this->ID))->getIDList();
$grandChildren = Page::get()->filter(array(
'ParentID' => $ids
));

return $grandChildren;
}

Maybe this is the wrong / complicated way to go about it. Any ideas appreciated.