We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12110
    • 122 Posts
    Hello,

    I enhanced the DynContent snippet to get content from some child documents into the parent doc:

    # Id of parent folder from snippet call
    $start = $startId;
    
    # depth 10
    $children = $modx->getChildIds($start, 10);
    
    foreach ($children as $key => $value) {
    
    $doc= $modx->getDocumentObject('id',$value);
    $output = str_replace(array("[!","!]"),array(""),$doc['content']);
    $output = $modx->parseDocumentSource($output);
    $outputcombined .= $output;
    }
    return $outputcombined; 
    


    So this is very handy to get content from child docs into the parent doc (where the snippet is called), and the child docs can be styled different from each oher. The goal is to have something similar to FlexibleContentElements as known of Typo3.

    This is working fine, but I have trouble with sorting the child docs - should be sorted by menuindex. I’m not the API hero and couldn’t sort it out so far - but I think it should not be to hard to do? Can somebody give me a hint?

    Thanks!
    Guenter
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      It cannot be sorted by anything else; the documentMap array that is used by the getChildIds function contains only the document ID and its parent, and is sorted by parent ID. If you want your list sorted by menuindex, you’ll have to use a SELECT query to get your list.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 12110
        • 122 Posts
        Hi Susan,

        thanks for your answer. I now realized what I wantet with MySQL - here’s the code I’using now:

        <?php
        
        # snippet DynContentAdv  v 0.1
        # based on DynContent Snippet by folibis
        # get content from different child docs of id $start
        # even if styled different or with different templates
        # sorted by menuindex
        # Version 0.1 by 7media.de - July 2009
        
        $start = $startId;
        if ($items == '') $items = 10;
        
        # Example call [!DynContentAdv? &startId=`10` &items=`10`!]
        
        $children = $modx->db->select('*','modx_site_content','parent = '.$start.' AND published = 1','menuindex ASC','0,'.$items.'');
        
        while ($childdoc = $modx->db->getRow($children)) {
        $output = "<div class=\"fce\">";
        $output .= "<h3>" . str_replace(array("[!","!]"),array(""),$childdoc['longtitle']) . "</h3>";
        $output .= str_replace(array("[!","!]"),array(""),$childdoc['content']); 
        $output .= "</div>"; 
        $output = $modx->parseDocumentSource($output);
        $outputcombined .= $output;
        }
        return $outputcombined;
        ?>


        It works fine, maybe the SELECT statement should query only selected columns. At this moment the elements are shown one below the other. One could imagine styling the div.fce or adding individual classes so that other layouts are possible. Other improvements?

        So this is working as solution to FlexibleContentElements for me.