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

    I needed to find a way of only calling Wayfinder for a submenu if the current document had children, otherwise I wanted to offer some alternative content. My approach was to use 2 chunks...1 for if the snippet (my first ever snippet btw ;-)) found children (this chunk simply contains [!Wayfinder!] and one for no children...this simply contains the latest Blog entry. I’m not sure how to improve it...I don’t like

    if($noChildData=='empty'){
    $noChildData = 'Document has no children';
    }else{
    $noChildData='{{'.$noChildData.'}}';
    }


    but couldn’t think of another solution to allow me to reference a chunk and leave a default value if no chunk is specified.

    The snippet looks likes this:
    // A simple snippet (my first) to check whether the current document has children
    // I created it for use with a sub navigation. If the document has children
    // I call a chunk that contains [!Wayfinder!] otherwise I output a HTML chunk
    // that contains something else...in my case the most recent blog entry
    // Call it like 
    // [[hasChildren? &noChildData=`noKidsChunk`&childData=`foundKidsChunk`]]
    // The chunks you define in this call omit the {{ and }}...I add them in the code
    // for the main resaon that it wouldn't work otherwise
    // In it's simplest form you can call [[hasChildren]] which will return a string
    // 'Document has no children' or 'This document has children' depending.
    // ------------------------
    
    $noChildData = isset($noChildData) ? $noChildData:'empty';
    $childData = isset($childData) ? $childData:'empty';
    if($noChildData=='empty'){
    $noChildData = 'Document has no children';
    }else{
    $noChildData='{{'.$noChildData.'}}';
    }
    if($childData=='empty'){
    $childData = 'This document has children';
    }else{
    $childData='{{'.$childData.'}}';
    }
    
    //Find children
    // This line taken from the ListChildOrSibs by [email protected]
    // so its probably the only one that is correct :-)
    $children = $modx->getDocumentChildren($modx->documentIdentifier,1,0,'hidemenu', 'hidemenu=0');
    
    // If there are no children for the current page output negative 
    if (!$children){
     $lcosOutput.= $noChildData;
    }else{
    //otherwise output positive
     $lcosOutput .= $childData;
    }
    // end if children exist
    
    // send to parser
    return $lcosOutput;


    I’ll look forward to any pointers in my (probably) very unorthodox coding smiley

    Cheers,
    Taff
      Adrian Lawley: www.adrianlawley.com
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Might be better to use $modx->getChunk(’chunkName’) than the tags, which are meant to be used in HTML code. Otherwise, if it works, it’s the "MODx way"!
        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
        • 29181
        • 480 Posts
        OK, I’m not sure why but initially it didn’t want to work, does now though

        <?php
        // ------------------------
        // Snippet: hasChildren
        // ------------------------
        // [email protected]
        //
        // Thanks to ListChildOrSibs by [email protected] for giving me a basic understanding
        // of how to accompish this

        // A simple snippet (my first) to check whether the current document has children
        // I created it for use with a sub navigation. If the document has children
        // I call a chunk that contains [!Wayfinder!] otherwise I output a HTML chunk
        // that contains something else...in my case the most recent blog entry
        // Installation:
        // Go to Resource => Manage Resources => Snippets & select "New Snippet"
        // Paste the contents of this txt file into it and save
        // Call it like
        // [[hasChildren? &noChildChunk=`noKidsChunk`&childChunk=`foundKidsChunk`]]
        // The chunks you define in this call omit the {{ and }}...I add them in the code
        // for the main resaon that it wouldn’t work otherwise
        // In it’s simplest form you can call [[hasChildren]] which will return a string
        // ’Document has no children’ or ’This document has children’ depending.

        //Check if Chunks were assigned as parameters when the snippet was called
        //If not pass it a value of ’empty’
        $noChildChunk = isset($noChildChunk) ? $noChildChunk:’empty’;
        $childChunk = isset($childChunk) ? $childChunk:’empty’;

        //If parameters weren’t passed then output a string telling the user
        //whether the current document has children or not
        if($noChildChunk==’empty’){
        $none = ’Document has no children’;
        }else{
        $none=$modx->getChunk($noChildChunk);
        }
        if($childChunk==’empty’){
        $some = ’This document has children’;
        }else{
        $some=$modx->getChunk($childChunk);
        }

        //Find children
        // This line taken from the ListChildOrSibs by [email protected]
        // so its probably the only one that is correct smiley
        $children = $modx->getDocumentChildren($modx->documentIdentifier,1,0,’hidemenu’, ’hidemenu=0’);

        // If there are no children for the current page output negative
        if (!$children){
        $hc_Output=$none;
        }else{
        //otherwise output positive
        $hc_Output = $some;
        }
        // end if children exist

        // send to parser
        return $hc_Output;
        ?>

        I submitted it to the resources too. http://modxcms.com/hasChildren-0.2-1457.html

        Think I will have to brush up on my PHP a little.

        Thanks,
        Taff
          Adrian Lawley: www.adrianlawley.com
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You want to list the children, or simply determine if there are children or not? To just find that out, there is a database field "is_parent", if that’s set to 1 then it has children.
            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
            • 29181
            • 480 Posts
            My plan was to just find out if it had children.

            I only want to display them though if the "show in menu" thingy is checked. That was my initial problem when I used getActiveChildren or whatever it was called.

            Cheers,
            Taff
              Adrian Lawley: www.adrianlawley.com
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Well, you could use a snippet that checks the is_parent field of document $docId, and if it is a parent, use
              $modx->runSnippet('Wayfinder', array('startId' => $docId, 'option' => 'value' ... ); 

              if it does have children. Wayfinder will already filter on the show in menu status for you.
                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
                • 29181
                • 480 Posts
                Thanks, I will look into that
                  Adrian Lawley: www.adrianlawley.com