Topic Tree - Number of Children
Permalink$topic->getTreeNodeChildCount(); count($topic->getChildNodes());
Both return 0. Every other method I try works as advertised. Something wrong here, or bug maybe?

Here are some code snippets to try. They work, but they make a lot of database queries, so I imagine there is a more efficient way to do it.
get the number of pages a topic has been added to
use Concrete\Core\Tree\Node\Type\Topic; $pageList = new PageList(); // get a topic by name or by ID // $topic is an instance of Concrete\Core\Tree\Node\Type\Topic // $topic = Topic::getByID(2); $topic = Topic::getNodeByName('Projects'); if ($topic) { $pageList->filterByTopic($topic); echo sprintf('%s %s <br>', $topic->getTreeNodeDisplayName(), $pageList->getTotalResults()); }
get the child topics of a topic tree and how many pages they have been added to (1 level)
- this doesn't get child categories of a topic tree, just direct child topics
use Concrete\Core\Tree\Type\Topic as TopicTree; use Concrete\Core\Tree\Node\Type\Topic; $topicTree = new TopicTree(); // get a topic tree by name or ID // $tree is an instance of Concrete\Core\Tree\Type\Topic // $tree = $topicTree->getByID(4); $tree = $topicTree->getByName('Blog Entries'); if ($tree) { // $treeNode is an instance of Concrete\Core\Tree\Node\Type\Category $treeNode = $tree->getRootTreeNodeObject(); // $childNodeIDs is an array of child IDs $childNodeIDs = $treeNode->getAllChildNodeIDs(); if ($childNodeIDs) { foreach($childNodeIDs as $childNodeID) { $pageList = new PageList();
get the child topics of a topic tree and how many pages they have been added to (2 levels)
- this will get the direct child topics and direct child topics of a direct child category
use Concrete\Core\Tree\Type\Topic as TopicTree; use Concrete\Core\Tree\Node\Type\Topic; $topicTree = new TopicTree(); // get a topic tree by name or ID // $tree is an instance of Concrete\Core\Tree\Type\Topic // $tree = $topicTree->getByID(4); $tree = $topicTree->getByName('Blog Entries'); if ($tree) { // $treeNode is an instance of Concrete\Core\Tree\Node\Type\Category $treeNode = $tree->getRootTreeNodeObject(); // $childNodeIDs is an array of child IDs $childNodeIDs = $treeNode->getAllChildNodeIDs(); if ($childNodeIDs) { foreach($childNodeIDs as $childNodeID) { $pageList = new PageList();
get all topic trees
- you can then loop over the topic trees to get the topic and topic categories
use Concrete\Core\Tree\Type\Topic as TopicTree; $topicTree = new TopicTree(); // get all topic trees // $trees is an array of Concrete\Core\Tree\Type\Topic instances $trees = $topicTree->getList(); if ($trees) { foreach ($trees as $tree) { echo $tree->getTreeName() . '<br>'; $treeNode = $tree->getRootTreeNodeObject(); $childNodeIDs = $treeNode->getAllChildNodeIDs(); } }
Thanks for taking the time with this! I seem to get the error: "Call to a member function getChildNodes() on null". Does it matter if it's in a page template and not a page list template?
EDIT: I got it now. I was putting in the handle not the name.
getTreeNodeParentObject()->getTreeNodeChildCount() returns 4
Then why is:
getTreeNodeParentObject()->getChildNodes() returning 'array[0]' ?
I expected an array of four treenodes or similar.
use \Concrete\Core\Tree\Node\Node as TreeNode; $ch = $user_provider[0]->getTreeNodeParentObject()->getAllChildNodeIDs(); foreach($ch as $item) { $node = TreeNode::getByID($item); ....
But unless getChildNodes() is poorly named it wouldn't seem to behave as expected?
It's worth noting that if you have:
MyNode
----topic
----topic
----topic
----category
--------topic2
Then getAllChildNodeIDs will also return topic2 (as well as category id but you can filter that with instanceof)
I can't find any way yet to get non-recursive children of the node so now I need to find a reliable method to filter out children of children which is burning a lot of time on what should be a simple task.
EDIT: I've literally just accidentally discovered populateDirectChildrenOnly() (by looking through the code - a poor way to discover things) which will load child nodes and cause getChildNodes() to work you'll get the 'category' entry but just filter that with instanceof.