Display Top Level Parent Name

Permalink 1 user found helpful
I'm trying to find the best way to dynamically insert the top level parent name for a side navigation.

The code I'm using (below) seems to work fine. However, when I put it into a stack it freezes Concrete and I can no longer access the stack. As soon as I remove the code below I can access the stack again.

I'm just looking for a way to echo the top level parent name from within a stack.

My current code is below:

$p = $c->getCollectionParentID();
function recurse($p,$id){
$value = $id;
if($p!=1){
$previd = $p;
$x = Page::getByID($p);
$p = $x->getCollectionParentID();
return(recurse($p,$previd));
}
else{
return $value;
}
}
$p = recurse($p,$c->getCollectionID());

$p = Page::getByID($p);
echo "<a href='".$this->url('/').substr($p->getCollectionPath(),1,strlen($p->getCollectionPath()))."/' ><h3>".$p->getCollectionName()."</h3></a>";

This seems to work fine when I use it outside of a stack (in an autonav template), but when I add the autonav to a stack everything seems to freeze.

Any help would be much appreciated. Thanks in advance!

roketto
 
Job replied on at Permalink Reply
Job
Can you better explain what you're trying to do? I'm sure there must be an easier way...
roketto replied on at Permalink Best Answer Reply
roketto
I did a little more searching and tried the solution provided by @Mainio in this post:http://www.concrete5.org/community/forums/customizing_c5/is-there-a...

// Set your level here
// 0 = the first level after home page level (home => CHILD)
// 1 = the second level (home => child => CHILD)
// etc.
$level = 0;
$current = $p = Page::getCurrentPage();
$tree = array();
while ($p->getCollectionParentID() >= HOME_CID) {
   array_push($tree, $p);
   $p  = Page::getByID($p->getCollectionParentID());
}
$tree = array_reverse($tree);
if (isset($tree[$level])) {
   $parent = $tree[$level];
   echo '<h2>' .  $parent->getCollectionName() .  '</h2>';


This seems to do the trick! And the code is much cleaner than what I had tried previously. Now when you to to stacks it just puts "Stacks & Blocks" as the top level parent name. But obviously that only displays as such when you're editing the stack.

Overall it seems to work great. Thanks @Mainio!
JohntheFish replied on at Permalink Reply
JohntheFish
If everything freezes, one possibility is you have inadvertently created an infinite recursion.

If you look at the code in Parent Area, that has similar functionality and also has a recursion breaker just in case (was added because of stacks being able to go round in an infinite loop).

http://www.concrete5.org/marketplace/addons/parent-area/...