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
Customize the default page title
Mar 12, 2025

Change the default " :: " and/or "site name :: page title" formatting separator to something else.

Configure Composer to work with a Page Type
Feb 20, 2025
By myq.

Fix the "Unable to load block into composer. You must edit this content from within the context of the page." error message

Permissions for editors in a multilingual site
Feb 2, 2025
By myq.

How to set up a multilingual Concrete CMS site for groups of language-specific editors

Restoring deleted pages using advanced search
Jan 16, 2025
By myq.

How to recover deleted pages when there are more than a few to choose from.

How to Automate the Copyright Year
Dec 27, 2024

Learn how to keep your website's copyright year updated automatically in Concrete CMS.

How to change the path of a group of pages
Dec 23, 2024
By myq.

Change the canonical path without breaking things

Improvements?

Let us know by posting here.