Topic Tree - Number of Children
Permalink
I'm attempting to display the number of children each topic contains. I've tried using these methods
Both return 0. Every other method I try works as advertised. Something wrong here, or bug maybe?
$topic->getTreeNodeChildCount(); count($topic->getChildNodes());
Both return 0. Every other method I try works as advertised. Something wrong here, or bug maybe?
Bump. I'm still struggling to show the number of articles in each topic. Any help is appreciated. Thanks!
Hi Kurtopsy,
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
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
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
get all topic trees
- you can then loop over the topic trees to get the topic and topic categories
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();
Viewing 15 lines of 25 lines. View entire code block.
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();
Viewing 15 lines of 42 lines. View entire code block.
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(); } }
Hi MrKDilkington,
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.
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.
On a related issue, if:
getTreeNodeParentObject()->getTreeNodeChildCount() returns 4
Then why is:
getTreeNodeParentObject()->getChildNodes() returning 'array[0]' ?
I expected an array of four treenodes or similar.
getTreeNodeParentObject()->getTreeNodeChildCount() returns 4
Then why is:
getTreeNodeParentObject()->getChildNodes() returning 'array[0]' ?
I expected an array of four treenodes or similar.
I worked around it like this:
But unless getChildNodes() is poorly named it wouldn't seem to behave as expected?
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?
Core Version - 8.4.2
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.
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.