So here’s the snippet
<?php
/**
* @name ParentParent
* @version 0.1
* @author Cipa
*
* @param &id The id of the document whose parent you want to find.
* @param &extraLevels Adds extra levels to the search.
*
* @license Public Domain, use as you like.
*
* @example [[ParentParent]]
* Will return the id of the parent of the parent for the current document
*
* @example [[ParentParent? &id=`30`]]
* Will return the id of the parent of the parent for document 30
*
* @example [[ParentParent? &id=`30` &extraLevels=`1`]]
* Will return the id of the parent of the parent of the parent for document 30
*
* This snippet travels up the document tree from a specified document and
* returns the parent id of the parent. To add an extra level to the search
* use the &extraLevels param.
* To be used with a least 3 levels of documents. If less the parent id or the document id will be returned
*
* Usage with wayfinder(note the [! !])
* [!Wayfinder? &startId=`[[ParentParent]]`!]
* [!Wayfinder? &startId=`[[ParentParent? &extraLevels=`1`]]`!]
*
* Inspired by UltimateParent
*/
$id = isset ($id) && intval($id) ? intval($id) : $modx->documentIdentifier;
$extraLevels = isset ($extraLevels) && intval($extraLevels) ? $extraLevels : 0;
$levels = 2 + $extraLevels; //the default start level is 2 which corresponds to the parent of the parent
$parentsIds = array_values($modx->getParentIds($id)); //get parents and make numeric keys
$parentsIdsNo = count($parentsIds);
if(array_key_exists($levels-1, $parentsIds)){
return $parentsIds[$levels-1];
}elseif($parentsIdsNo){
return array_pop($parentsIds);
}
return $id;
?>
I have a dilemma though. When used with Wayfinder like [!Wayfinder? &startId=`[[ParentParent]]`!] there is no way to cache the Wayfinder call.
Which is very bad in my opinion. Are there any ways I can "register" the [[ParentParent]] output into the modx object so I can do something like
[[Wayfinder? &startId=`[*parentparent*]`]] where parentparent is a variable that is global set up by ParentParent snippet.
One can call the 2 snippets sequentially
[[ParentParent]] <- will set up a global parentparent variable
[[Wayfinder? &startId=`[*parentparent*]`]] <- Wayfinder knows about parentparent
Thx