We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12151
    • 115 Posts
    greetings ...

    is it possible to determine whether one or more of the children of any given document are weblinks instead of documents?

    for example i have a structure like this:

    - About Us
    - External Link 1 (weblink)
    - External Link 2 (weblink)
    - The Team (document)

    i want to have a snippet that will know that there are child weblinks (using getDocumentChildren?) and show them in a chunk template (using Ditto?) but if there are no weblinks then do nothing.

    any ideas greatly appreciated! smiley

      • 12151
      • 115 Posts
      SOLVED

      if anyone is interested, or if anyone has a better solution, i finally came up with the following solution which displays a formatted box for external links, if the current document has child weblinks:

      for each child weblink created i added "weblink" as the description then called the following snippet in my template

      $thisID = $modx->documentObject['id'];
      $children = $modx->getDocumentChildren($thisID);
      
      $NumWebLinks = 0;
      $ChildIDs = '';
      
      foreach ($children as $child) {
        if($child['description']=='weblink'){ 
          $NumWebLinks ++; 
          $ChildIDs .= $child['id'].',';
        }
      }
      
      if($NumWebLinks >0){
        $params['tpl'] = 'WebLinks';
        $params['documents'] = substr($ChildIDs, 0, strlen($ChildIDs)-1);
        $params['display'] = 'all';
       
        $prehtml = $modx->getChunk('ExternalLinksTop'); 
        $html = $modx->runSnippet('Ditto', $params);
        $posthtml = $modx->getChunk('ExternalLinksBottom'); 
      
        $output = $prehtml.$html.$posthtml;
        return $output;
      }
        • 14412
        • 74 Posts
        Hi, just bumped into your question. Had exactly the same question the other day and came up with this snippet:

        <?php
        //Snippet WebLinksList
        //Container Document, defaults to site root (work through full doc tree) if none given
        $container = isset($container) ? $container : 0;
        //Depth to search for children documents, defaults to 1 if none given
        $depth = isset($depth) ? $depth : 1;
        //Fetch Children documents
        $res=$modx->getChildIds($container, $depth);
        $linklist = '';
        foreach ($res as $key=>$value){
        	$doc=$modx->getDocument($key);
        	if($doc['type']=='reference'){
        		if ($linklist != '') $linklist .= ',';
        		$linklist .= $doc['id'];
        	}
        }
        return $result;
        ?>
        


        It just outputs a comma-separated list of all the weblinks under the $container doc, up to the $depth depth. As you can see, no need to set the description of the links to ’weblink’ - there’s already a field (type=reference) for that on the modx_site_content table.

        The actual output of the weblinks on your pages can be done in many ways, i guess you can pick up from this.For instance using Ditto 2:

        [tt][[Ditto?documents=`[!WebLinksList?container=`21`!]` &tpl=`ExternalLinks`]][/tt]

        Styling the list on the ExternalLinks chunk.
          Got Music? Try XSPFjukebox - Skinnable MODx Media Player
          • 12151
          • 115 Posts
          a very nice solution ... thanks for sharing smiley