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

Loading Conversation