We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32241
    • 1,495 Posts
    ADDED TO REPOSITORY

    This is a snippet to fetch certain fields from parent document. It requires the latest docAPI library that I’ve posted on this forum as well. It doesn’t take too much processing time, when it’s called multiple times due to caching feature implemented to this snippet.

    Hope it might prove to be useful.

    Sincerely,
    Wendy Novianto
    DJAMOER

    <?php
    /**
     * GetParent
     * Get the parent object from the current document parent id. If there
     * is no parent, it will return empty. This snippet will not fetched the
     * parent object multiple times when it's called multiple times in the
     * document.
     *
     * Parameters:
     * - $field - Name of the document/template variable to be outputed
     * - $formatted - Set it to true to output the TV in formatted output, default false
     * - $level - Start from 0 to infinity
     */
    
    if(!isset($field)) return '';
    else $field = trim($field);
    
    if(!isset($level) || !is_numeric($level)) $level = 0;
    else $level = (int) $level;
    
    if(isset($formatted) && $formatted) $formatted = true;
    else $formatted = false;
    
    include_once('assets/libs/docapi/docapi.class.inc.php');
    
    if(isset($modx->parentDocObject[$level]) && is_array($modx->parentDocObject[$level])) {
    	if(isset($modx->parentDocObject[$level][$field])) return ($formatted) ? $modx->parentDocObject[$level][$field.'_output'] : $modx->parentDocObject[$level][$field];
    }
    else {
    	$objectDocAPI = new docAPI();
    	for($i = 0; $i <= $level; $i++) {
    		if(!isset($modx->parentDocObject[$i]) || !is_array($modx->parentDocObject[$i])) {
    			if($i == 0) $parent = $modx->documentObject['parent'];
    			else $parent = $modx->parentDocObject[$i-1]['parent'];
    			if($parent > 0) {
    				$docObject = $objectDocAPI->staticGetDocuments($parent);
    				if(is_array($docObject) && count($docObject) == 1) {
    					$modx->parentDocObject[$i] = $docObject[0];
    				}
    			}
    			else $i = $level + 1;
    		}
    	}
    	if(isset($modx->parentDocObject[$level][$field])) return ($formatted) ? $modx->parentDocObject[$level][$field.'_output'] : $modx->parentDocObject[$level][$field];
    }
    return '';
    ?>
    
      Wendy Novianto
      [font=Verdana]PT DJAMOER Technology Media
      [font=Verdana]Xituz Media
      • 6726
      • 7,075 Posts
      Another great one Wendy, thanks !

      For those who (like me) would need to download the docAPI library, here is the link :
      http://modxcms.com/forums/index.php/topic,4031.msg29385.html#msg29385 smiley
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l&#39;outil id
        • 33175
        • 711 Posts
        Thanks for this Wendy smiley
          Sorry for my english. I&#39;m french... My dictionary is near me, but it&#39;s only a dictionary !
          • 32241
          • 1,495 Posts
          I just modified the code above, added $level parameter, allowing the snippet to fetch parent folder from several level. 0 will be the first level, 1 will be the second level (parent from current document parent), and so on. Fix the caching mechanism as well.

          PS: I just notice there is a snippet called GetParentField. Maybe somebody combine both functionality.

          @David, thanks for pointing that out
          @Guillaume: Ur welcome...
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media