Get topic node in display order

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
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.

How to update Add-Ons if not on the Update Add-Ons Menu item
Jul 4, 2022

How to manually download an add-on and update it when your site's core versions isn't considered compatible with the add-on version.

Generate a report with author information and form summaries in Concrete CMS.
May 9, 2022

In Concrete CMS, you can use a form to initiate contact between logged-in users and then create helpful reports. After form submissions are collected, they can be searched, sorted, and exported as a spreadsheet. This tutorial will detail how to add author information to a report using the advanced search.

How to clone and customize Atomik theme
Feb 14, 2022
By linuxoid.

How to clone and customize Atomik theme

Update jQuery to 3.5 on Concrete CMS version 8.5.x
Dec 1, 2021
By hissy.

If you have to take some time to fix your site to work with version 9 and want to update jQuery immediately, you can override it.

Was this information useful?
Thank you for your feedback.