We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28338
    • 46 Posts
    I’ve implemented a &topLevel feature. Try it if you will. I don’t promise anything smiley

    // UltimateParent - snippet for MODx 0.9x
    // Travels up the document tree from the current document
    // to return the "ultimate" parent
    // March 2006 - [email protected]
    // Bug fix Sept 17, 2006 - Jason Coward <[email protected]>
    // topLevel feature added Sept 21, 2006 - Anders Milton <[email protected]>
    // Released to the Public Domain, use as you like
    // arguments:
    // &id       - the id of the document whose parent you want to find
    // &top      - the top of the search 
    // &topLevel - the top level of the search. &top must be undefined or set to 
    //             zero
    // examples:
    // [[UltimateParent? &id=`45` &top=`6`]]
    // will find the first parent of document #45 under document #6
    // [[UltimateParent? &id=`45` &topLevel=`3`]]
    // will find the first parent of document #45 on level 3
    // if id == 0 or top == id, will return id.
    // you can use this as the startDoc for DropMenu to create specific submenus.
    
    $top = isset($top)?$top:0;
    $id = isset($id)?$id:$modx->documentIdentifier;
    if($top==0) { $topLevel = isset($topLevel)?$topLevel:1; } else { $topLevel=0; }
    
    $levelIds = array();
    $levelIds[0]=$id;
    
    if($id==$top || $id==0) { return $id; }
    $pid = $modx->getParent($id,1,'id');
    
    for($i=1; $pid['id'] != $top; $i++) {
    	$id = $pid['id'];
    	$levelIds[$i]=$id;
    	$pid = $modx->getParent($id,1,'id');
    	if($pid['id'] == $top && $topLevel == 0) { return $id; }
    }
    
    // If &topLevel is set
    if($topLevel != 0 && $top == 0) {
    	$levelIds = array_reverse($levelIds);
    	return $levelIds[$topLevel-1];
    }
    
    return 0; // if all else fails