We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27656
    • 5 Posts
    Hello everyone! You guys rock!

    Here is my first post here and also first attempt to contribute to MODx community wink

    RandChildrenTVGetter v. 0.0.1 BETA

    Description: This snippet gets TVs/resourceField values of one or several random children of chosen resource and outputs them in specified chunk.

    It’s very simple, but I hope somebody will still find it useful.

    <?php
    //    RandChildrenTVGetter 
    
    //    v. 0.0.1 BETA
    
    //    description:
    //        Aggregates TVs/resourceField values of one or several random children of chosen resource  and outputs them in specified chunk
    // 
    //    author: 
    //        Alex 'Silkworm' Pronin
    //
    
    //    ------------parameters-------------
    
    //    &tpl - chunk to use for output 
    //    Note: !!!won't return anything if not specified!!!
    
    //    &parentid - ID of parent resource
    
    //    &childnum - number of children which TVs to output
    
    //                    default - 1
    
    //    &tvstring - comma separated string of TVs/ 
                            
    //                    default - pagetitle,content
     
    //    &published -    1 show published resources (default)
    //                    0 - show unpublished resources
    
    //    &prefix - prefix to identify snippet's placeholders 
    //                        
    //                    default - rnd_
    //
    //    Note: !!!Two identical placeholder names in one template can become a mess, so it's healthier IMHO to attach a prefix!!!
    
      
        if (!isset($parentid))
            $modx->documentIdentifier;
        if (!isset($prefix) || $prefix="")
            $prefix = "rnd_";
        if (!isset($tvnames))
            $tvnames = 'pagetitle,content';
        if (!isset($childnum))
            $childnum = 1;
        if (!isset($published))    
            $published = 1;
        
            
        $TVNamesArr = explode(",",$tvnames);
        $TVout = $modx->getDocumentChildrenTVarOutput($parentid,$TVNamesArr,$published);
        if(!$TVout && !isset($tpl)){
            $output = false;
        }
        else {
            $childnum = count($TVout) < $childnum ? count($TVout) : $childnum;
            $RandChildId = array_rand($TVout,$childnum);
            if (!is_array($RandChildId)) $RandChildId = (array)$RandChildId;  
            for ($i=0; $i<$childnum; $i++) {
                foreach ($TVNamesArr as $TVname) {
                    $TVvalues[$prefix.$TVname] = $TVout[$RandChildId[$i]][$TVname];                          
                }
                $output .= $modx->parseChunk($tpl, $TVvalues, '[+', '+]');             
            }         
       }
       return $output;
    ?>


    Usage:

    1) Create a new snippet RandChildrenTVGetter and past snippet’s code, save;

    2) Create a new chunk ChildTVtpl;

    3) Specify the placeholders of this chunk like this: prefixTVname, e.g if default prefix rnd_ is used it would be
    [+rnd_pagetitle+]
    or
    [+rnd_longtitle+]


    4) Create snippet call wherever you want, e.g.
    [!RandChildrenTVGetter? &parentid=`10` &tvnames=`pagetitle,longtitle` &childnum=`2` &published=`0` &tpl=`ChildTVtpl`!]


    Notes:

    1) This snipped should be called uncached in order to work properly;

    2) Remember to specify template (&tpl parameter) in your snippet call otherwise it won’t work;

    3) Don’t use spaces between tv names in &tvnames parameter string, only commas.

    This is beta, so testing and feedbacks are appreciated.

    Cheers,

    Silkworm