Get topic node in display order

This is a community-contributed tutorial. This tutorial is over a year old and may not apply to your version of Concrete CMS.
Jun 8, 2016

When calling the topic node from your page attribute programmatically, you are only able to get the topics in order of registration, not display order. This tutorial explains how to sort the topic node in display order.

Condition

  • Checked with Concrete CMS 5.7.5.6~8
  • Go to [Dashboard] - [System and Settings] - [Attributes] - [Topics]. Then create news category topic.
    • Register new topics in order of "News", "Press Release", "Recruit".
    • Drag and drop the topics to change the order to "Recruit", "News" and "Press Release".
  • Go to [Dashboard] - [Page and Theme] - [Attributes], then create "News" (news_type) topic attribute
  • Create "News" page tyoe with News topic attribute.
  • Create a page with News page type.
    • Choose all of Recruit, News, and Press Release topics, then save the page.
  • Create a page template or block custom template to display the selected topics using the following sample code.

Sample Code

The following is the code snippet to display the selected topics of the page.

<?php
$topics = $c->getAttribute('news_type');
if (is_array($topics)) {
    if (count($topics)) {
        foreach($topics as $topic) {
            echo "`" . $topic->getTreeNodeDisplayName() . "`,";
        }
    }
}
?>

(If this is page list block, change from $c->getAttribute('news_type') to $page->getAttribute('news_type') )

Problem

You want to display the topics in order of your choice. However, it only displays in order of registration.

Solution

Let's change the order of $topics by adding additional codes:

<?php
function orderNode($a, $b) {
    return strcmp($a->treeNodeDisplayOrder, $b->treeNodeDisplayOrder);
}


$topics = $c->getAttribute('news_type');
if (is_array($topics)) {
    if (count($topics)) {
        usort($topics, "orderNode");

        foreach($topics as $topic) {
            $newscategory = $topic->getTreeNodeDisplayName();
        }
    }
}
?>

by adding usort and orderNode() function.

Cause

Why it happens like this? Let's dump the $topics array.

<?php
$topics = $c->getAttribute('news_type');
var_dump($topics);
?>

It would show up like the following.

array(3) {
  [0]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#1893 (13) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["tree":protected]=>
    NULL
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "10"
    ["treeNodeTypeID"]=>
    string(1) "3"
    ["treeID"]=>
    string(1) "1"
    ["treeNodeParentID"]=>
    string(2) "1"
    ["treeNodeDisplayOrder"]=>
    string(1) "1"
    ["treeNodeOverridePermissions"]=>
    string(1) "0"
    ["inheritPermissionsFromTreeNodeID"]=>
    string(2) "1"
    ["treeNodeTopicName"]=>
    string(12) "News"
  }
  [1]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#1889 (13) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["tree":protected]=>
    NULL
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "11"
    ["treeNodeTypeID"]=>
    string(1) "3"
    ["treeID"]=>
    string(1) "1"
    ["treeNodeParentID"]=>
    string(2) "1"
    ["treeNodeDisplayOrder"]=>
    string(1) "2"
    ["treeNodeOverridePermissions"]=>
    string(1) "0"
    ["inheritPermissionsFromTreeNodeID"]=>
    string(2) "1"
    ["treeNodeTopicName"]=>
    string(12) "Press Release"
  }
  [2]=>
  object(Concrete\Core\Tree\Node\Type\Topic)#1888 (13) {
    ["childNodes":protected]=>
    array(0) {
    }
    ["childNodesLoaded":protected]=>
    bool(false)
    ["treeNodeIsSelected":protected]=>
    bool(false)
    ["tree":protected]=>
    NULL
    ["error"]=>
    string(0) ""
    ["treeNodeID"]=>
    string(2) "12"
    ["treeNodeTypeID"]=>
    string(1) "3"
    ["treeID"]=>
    string(1) "1"
    ["treeNodeParentID"]=>
    string(2) "1"
    ["≈"]=>
    string(1) "0"
    ["treeNodeOverridePermissions"]=>
    string(1) "0"
    ["inheritPermissionsFromTreeNodeID"]=>
    string(2) "1"
    ["treeNodeTopicName"]=>
    string(8) "Recruit"
  }
}

"treeNodeDisplayOrder" in the object are set in otder, but the array stores only in order of registration.

Recent Tutorials
Using the Concrete Migration Tool Addon
Apr 27, 2023

How to use the Concrete CMS Migration Tool

How To Add Page Last Updated To Your Concrete CMS Pages
Mar 7, 2023

Concrete CMS has a page attribute you can add to a global area called "Page Date Modified." Here's how to add it

How To Exclude Subpages from Navigation
Dec 24, 2022

How to exclude subpages from navigation - useful for a news or blog link in your main navigation where you don't want all the subpages to appear in a drop down menu.

How Can I Change The Maximum Size Of Uploaded files
Dec 13, 2022

This tutorial explains how to update your php settings.

Updating Concrete Themes from Version 8 to Version 9
Nov 24, 2022

This tutorial covers commonly encountered issues when upgrading a Concrete CMS theme from version 8 to version 9

Transferring ownership of an add-on and a theme
Nov 15, 2022
By katzueno.

If you encounter a Concrete CMS add-on or theme that you love but not being maintained, you may want to ask the author to help or take over the add-on or theme. Here is the quick step-by-step guide of how to transfer the ownership.

Was this information useful?
Thank you for your feedback.