We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6902
    • 126 Posts
    I made some modifications to UltimateParent--I just wanted a quick way to be able to return the page title instead of the ID. I shamelessly lifted some code from the getResourceField thread elsewhere on the forums (thanks SplittingRed!).

    I also went ahead and "cleaned up" the code a bit adding added more examples and comments.

    Here it is if anyone is interested! grin
    <?php
    /**
     * @name UltimateParent
     * @version 1.4
     * @author Susan Ottwell <[email protected]> March 2006
     * @editor Jason Coward <[email protected]> Sept 17, 2006
     * @editor Al B <> May 18, 2007
     * @editor S. Hamblett <[email protected]>
     * @editor Shaun McCormick <[email protected]>
     * @editor Darren Doyle <[email protected]>
     *
     * UltimateParent - snippet for MODx 0.9x Travels up the resource tree from the
     * current resource to return the "ultimate" parent
     *
     * March 2006 - [email protected]
     * Bug fix Sept 17, 2006 - Jason Coward
     * Bug fix to prevent infinite loops if parent never = $top. 18 May, 2007 - Al B
     * Released to the Public Domain, use as you like November 2008 converted for
     * use with Revolution by S. Hamblett
     * More examples, commented code & added ability to return pagetitle - June 1, 2010 - Darren Doyle
     *
     * ARGUMENTS:
     * $id - the id of the resource whose parent you want to find. default: current resource id
     * $top - the top of the search. default: 0
     * $title - return pagetitle instead of id? default: 0
     *
     * EXAMPLES:
     * [[UltimateParent]]
     * will return the id of the current resource's ultimate parent
     *
     * [[UltimateParent? &id=`45` &top=`6`]]
     * will return the id of the ultimate parent of resource #45 under resource #6
     *
     * [[UltimateParent? &id=`45` &top=`6` &title=`1`]]
     * will return the page title of the ultimate parent of resource #45 under resource #6 
     *
     * You can use this as the startDoc for DropMenu to create specific submenus.
     */
    
    // INITIALIZE
    $output = 0;
    $top = isset($top) ? $top : 0;
    $id = isset($id) ? $id : $modx->resource->get('id');
    $title = isset($title) ? $title : 0;
    
    // check to see if the current resource is at the top
    if ($id == $top || $id == 0) {
    	$output = $id;
    
    // otherwise...
    } else {
    
    	// get the immediate parent of the current resource
    	$pid = $modx->getParent($id,1,'id');
    
    	// stop if the immediate parent is at the top
    	if ($pid['id'] == $top) {
    		$output = $id;
    	
    	} else {
    
    		// loop to find ultimate parent 
    		while ($pid['id'] != $top && $pid['id'] != 0) {
    			$id = $pid['id'];
    			$pid = $modx->getParent($id,1,'id');
    			if ($pid['id'] == $top) {
    				$output =  $id;
    				break;
    			}
    		} 
    	}
    }
    
    
    // return page title instead of id?
    if ($title) {
    	$c = $modx->newQuery('modResource');
    	$c->select($modx->getSelectColumns('modResource','modResource','','',array('id','pagetitle')));
    	$c->where(array(
    		'id' => $output
    	));
    	$resource = $modx->getObject('modResource',$c);
    	$output = $resource->get('pagetitle');
    }
    
    return $output;
    
    ?>