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 locale icons
Oct 29, 2024
By myq.

How to customize locale (language region) flags

Concrete CMS Caching Guide
Oct 16, 2024

An overview of types of caching in Concrete and considerations when using them.

Redirect all requests to HTTPS
Oct 9, 2024
By myq.

How to follow best practices for a secure web

Upgrade Concrete versions 9.3.1 and 9.3.2
Sep 10, 2024
By myq.

How to get past a bug in versions 9.3.1 and 9.3.2 that prevents upgrading the Concrete core through the Dashboard

How to use Composer with Marketplace extensions
Aug 22, 2024

Composer can be used to manage third-party extensions from the marketplace

Controlling Google Tag Manager Tags Based on Concrete CMS Edit Toolbar Visibility
Aug 13, 2024

This document provides a step-by-step guide on how to control the firing of Google Tag Manager (GTM) tags based on the visibility of the Concrete CMS edit toolbar. It explains how to create a custom JavaScript variable in GTM to detect whether the edit toolbar is present on a page and how to set up a trigger that ensures GTM tags only fire when the toolbar is not visible. This setup is particularly useful for developers and marketers who want to ensure that tracking and analytics tags are not activated during content editing sessions, thereby preserving the accuracy of data collected.

Improvements?

Let us know by posting here.