Page 1 of 1

UDT: Get Level Two Title

Posted: Fri Aug 03, 2018 12:26 pm
by paulhermans
Situation:

I'm updating a CMSMS website from 1.x to 2.x and there is a User Defined Tag that I need to rewrite.

I have a website with a FAQ: /faq/category/article
Level 1: faq
Level 2: category
Level 3: article

When on a level 3 page I want to display the "title" of the category above it.

This is the OLD UDT code:

Code: Select all

$manager = cmsms()->GetHierarchyManager();

$var = isset($params['assign']) ? $params['assign'] : 'root_page_alias';

$result = "NO RESULT";
$thisPage = $smarty->get_template_vars('content_id');
$currentNode = $manager->sureGetNodeById($thisPage);
while( isset($currentNode) && $currentNode->getLevel() >= 1 )
{
    $currentContent =& $currentNode->getContent();
    $result = $currentContent->Name();
    $currentNode =& $currentNode->getParentNode();
}
echo $result;
UDT: Get_Level_Two_Alias

This code might be hulpful, it gets the alias of level 2.

Code: Select all

$hm = cmsms()->GetHierarchyManager();
if ( empty($alias) ) $alias = \cms_utils::get_current_alias();
$stack = array();
$node = $hm->find_by_tag('alias',$alias);
while( $node && $node->get_tag('id') > 0 )  {
  $stack[] = $node;
  $node = $node->getParent();
}
if( count($stack) == 0 ) return;
$alias = $stack[count($stack)-2]->get_tag('alias');
echo $alias;
Does anybody know how to get the corresponding Title?

Re: UDT: Get Level Two Title

Posted: Fri Aug 03, 2018 2:28 pm
by calguy1000
https://apidoc.cmsmadesimple.org/classe ... tBase.html

You want the page name.. or the title attribute.

Re: UDT: Get Level Two Title

Posted: Fri Aug 03, 2018 2:42 pm
by paulhermans
Hi i'm looking for the title attribute of the page one level up in the hierarchy.

Do you know how I can get this value?

Re: UDT: Get Level Two Title

Posted: Fri Aug 03, 2018 2:56 pm
by DIGI3
CGSimpleSmarty has functions for that, something like:

Code: Select all

{cgsimple::get_page_title(cgsimple::get_parent_alias())}
Should work.

Re: UDT: Get Level Two Title

Posted: Fri Aug 03, 2018 3:52 pm
by paulhermans
Thanks DIGI3, that works, but only if you are at level 3.

Do you also have a solution that gets me the title of level 2 no matter if I am at level 2 or 3?

Re: UDT: Get Level Two Title

Posted: Fri Aug 03, 2018 9:28 pm
by DIGI3
(cgsimple::get_parent_alias()) gets the parent of the current page. There's also a function for getting the root alias, it's in the CGSS docs.

You can stack them if you're looking for the grandparent, great-grandparent, etc. e.g. (cgsimple::get_parent_alias((cgsimple::get_parent_alias()))) should get the grandparent.