How to get a value of a topic (array)

hi all

i am trying to get one single value of an array, if i day print_r it says:

Array
(
    [0] => Concrete\Core\Tree\Node\Type\Topic Object
        (
            [childNodes:protected] => Array
                (
                )

            [childNodesLoaded:protected] => 
            [treeNodeIsSelected:protected] => 
            [tree:protected] => Concrete\Core\Tree\Type\Topic Object
                (
                    [treeNodeSelectedIDs:protected] => Array
                        (
                        )

                    [error] => 
                    [treeID] => 5
                    [treeTypeID] => 3
                    [treeDateAdded] => 2022-12-08 17:58:47
                    [rootTreeNodeID] => 30
                    [topicTreeName] => News Filter
                )

            [error] => 
            [treeNodeID] => 34
            [treeNodeTypeID] => 7
            [treeID] => 5
            [treeNodeParentID] => 30
            [treeNodeDisplayOrder] => 0
            [treeNodeName] => News
            [dateModified] => 2022-12-08 18:07:46
            [dateCreated] => 2022-12-08 18:07:46
            [treeNodeOverridePermissions] => 0
            [inheritPermissionsFromTreeNodeID] => 30
            [treeNodeTypeHandle] => topic
        )

)

if i try something like:

            <?php
            $array = $page->getAttribute('category');
            print_r($array);
            echo $array[10];
            ?>

to test it only says either nothing, NULL or “Array”.
But I would like to have the value of “treeNodeName”.

thank you for your inputs!

you can’t do that
maybe this piece of code may help you

function lesDisciplines($ladiscipline){
  $topicTree = TopicTree::getByName('Disciplines');
  $topicsRoot = TreeNode::getByID($topicTree->getRootTreeNodeObject()->treeNodeID);
  $topicsRoot->populateChildren();
  $csnTopics = $topicsRoot->getChildNodes();
      foreach ($csnTopics as $discipline) {
                                        echo '<option value="' . $discipline->treeNodeID . '"';
          if($discipline->treeNodeID==$_GET['discipline']){
              echo " selected";
          }
          echo '>'. $discipline->treeNodeName .'</option>';
                                }
}
1 Like

Your exemple array only has 1 element the one with key value of 0 so if you try to get an element with key equal to 10 you won’t get anything.

Now in the example you gave $array[0] is an object of type Concrete\Core\Tree\Node\Type\Topic so you can’t really do print_r on an object.

What you could do is

$topic = $array[0]; // getting the first element in the array, the one with index 0
// now $topic is an object of type Concrete\Core\Tree\Node\Type\Topic so you can use its public methods. Example:
echo $topic->getTreeNodeDisplayName();
echo $topic->getDateLastModified();

Super star!!! :slight_smile: thank you so much!

dear mnakalay
just one more question which came up – if a do not choose a certain page type (see attachment), it endsup in an error ( “Call to a member function getTreeNodeDisplayName() on null”).
so it only works with this page type. what if the client changes it accidentally?

I think you forgot the attachment you’re referring to so I’m not sure what the issue is.

i am so sorry, can you see it now?

and this happens if you create a new page and no topic is selected. if you publish the site immediately that’s not possible because topics are a must field. if you plan the site online time, you can “avoid” the must fields.

This is because you are asking to echo out the topic but it’s not there so crashes.

What I would do is isset() the topic

thank you very much!
but just one question, i also need then to echo something? i am confused… :face_with_peeking_eye:

Where you have you line with the <p class="page-list-category… Etc

Wrap the whole thing with

<?php

if isset($topic){ 

?>

<p .....


<?php } ?>

So what you are saying is that if $topic actually has something, produce the code.

thank you very much for this!
that helps, do you mind giving me a hint, how it needs to be if i have an if condition already?

if ($topic->getTreeNodeDisplayName() == "Monthly Report" && $downloadLink != 0) { ?>
<?php echo $textNewsDE ?>
<a class="btn secondary-blue-btn" href="<?php echo $downloadLink->getDownloadURL(); ?>" target="_blank">Download PDF</a>
<?php }

i somehow dont get it, what needs to be done, if no topic is delivered. can it be filled with a standard topic? i just want to avoid these failure screens…

Isset might not work here. Better check tht you have an object. Like this :

if (is_object($topic) && $topic->getTreeNodeDisplayName() == "Monthly Report" && $downloadLink != 0) { ?>
<?php echo $textNewsDE ?>
<a class="btn secondary-blue-btn" href="<?php echo $downloadLink->getDownloadURL(); ?>" target="_blank">Download PDF</a>
<?php }

thank you so much !
but do i need an else value?
sorry i am completely lost :face_with_peeking_eye:

Not really. If you have a topic and it is “monthly Report” and you have a download link, you will show a link to your file.

Otherwise you won’t show anything.

By the way you should probably modify the code to also check $downloadLink correctly. Like this

if (is_object($topic) && $topic->getTreeNodeDisplayName() == "Monthly Report" && is_object($downloadLink)) { ?>
<?php echo $textNewsDE ?>
<a class="btn secondary-blue-btn" href="<?php echo $downloadLink->getDownloadURL(); ?>" target="_blank">Download PDF</a>
<?php }