We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29635
    • 361 Posts
    Hi all. My first snippet that I feel is actually worth sharing. It seems like every site I do in MODx I want child docs to inherit TV values from their parents, so I made this snippet.

    How to use: Set [!InheritTV!] as the default TV value; whenever you set a document’s TV value, all the children of that document will "inherit" that TV value.

    It could be better: It only works for the parent doc and will not go up the tree any higher. So, if a document’s grandparent has a TV assigned, but the parent does not, it doesn’t work. (It actually returns [!InheritTV!].) If anybody can offer some advice to get that working it’d be swell.
    // CONFIGURABLE OPTIONS
    // Set the $tvIdNage to equal the TV you are inheriting.
    // Example:  The TV is called HeaderImg, so you'd find
    // the id of that TV, or call it by name
    // [!InheritTV?tvIdName=`HeaderImg`!]
    $tvIdName = (!isset($tvIdName)) ? '1' : "$tvIdName";
    // END CONFIG
    
    $id = $modx->documentIdentifier;
    $pid = $modx->getParent($id,1,"id");
    $pid = $pid['id'];
    
    $ptv = $modx->getTemplateVarOutput($tvIdName,$pid,1);
    
    $output = '';
    foreach($ptv as $feld => $value) {$output = $value;}
    return $output;
    

    The $id and $pid are from Susan’s UltimateParent. Also, I’m fully aware that my PHP sucks, so feel free to offer suggestions. grin
      Need MODx Ecommerce? Try FoxyCart!
      • 1764
      • 680 Posts
      Brett,

      There’s a new feature built into 0.9.1 that I believe will exactly what this Snippet is doing and it will also climb up the tree as far as it needs to to find content. It also allows you to set a default in case no content is found in any parent.

      @INHERIT documentation
        • 29635
        • 361 Posts
        Wow, yeah, that’s exactly what I was going for. Oh well, it was a good learning experience. wink

        I had a feeling that the solution existed, but I couldn’t find it. Thanks Adam.

        Edit: I guess the "InheritTV" name was pretty good, seeing as that’s what it’s called in the core! At least I got that right.
          Need MODx Ecommerce? Try FoxyCart!
          • 17705
          • 501 Posts
          but it would be nice to have something able to show rich text TVs...