We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26931
    • 2,314 Posts
    Hi everybody,

    guess there’s already a Snippet floating around on the forum for this smiley ...can’t find it though

    i’d like to return a list of IDs from all ressources (within a container) that are containers/folders themselves.

    i’m asking because i ran into this problem:
    http://modxcms.com/forums/index.php/topic,36667.msg289992.html#msg289992
    and a possible workaround would be this:
    http://modxcms.com/forums/index.php/topic,36667.msg290667.html#msg290667

    thanks, j
      • 3749
      • 24,544 Posts
      As you say, there may already be a snippet for this (I don’t know of one).

      If not, you could probably write one based on the getChildIds() code and only add IDs to the array if isfolder is set.

      http://wiki.modxcms.com/index.php/API:getChildIds
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 26931
        • 2,314 Posts
        thanks Bob, i’ll look into that
          • 26931
          • 2,314 Posts
          lol, okay ... it’s getting ugly - but it’s working (please comment if you got any improvements or ideas!):

          <?php
          // parent ID
          $ids="11";
          // how many levels deep
          $depth="5";
          
          // get all children
          $childArray = $modx->getChildIds($ids, $depth);
          
          // get IDs from all children that are containers
          $container = $modx->getDocuments($childArray, 1, 0, "id", 'isfolder=1');
          
          // count the IDs and loop through the array
          $childrenCount = count($container); 
          for($x=0; $x<$childrenCount; $x++) {
          $folderids .= $container[$x]['id'].",";
          }
          
          return $folderids;
          ?>


          huh unfortunately the other Snippet doesn’t like the output http://modxcms.com/forums/index.php/topic,36667.msg290667.html#msg290667
            • 3749
            • 24,544 Posts
            I think it’s because you’re returning an array and the parameter should be a comma-delimited list.

            Try this:

            return implode( ',',$folderids);
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 26931
              • 2,314 Posts
              Hi Bob,
              thanks
              return $folderids; 
              is already returning a comma separated list.
              btw. i found out, that if i use that Snippet within the other one ( link above) to return the parent IDs, i need to "print" the result instead of "return" smiley ...then it works
                • 26931
                • 2,314 Posts
                ... cleaned up the Snippet code a bit. i know, i know commented too much & the obvious smiley // just for my record

                <?php
                
                /*
                 * This Snippet will return a comma separated List of container IDs
                 * within a declared container.
                 * e.g. [[getChildContainer? &parent=`2` $depth=`2`]]
                 * btw. "print" to return the value is used because i call the Snippet within 
                 * another Snippet. you can change "print" to "return" if you're not.
                 */
                
                // use parent ID if declared, otherwise default to 0 (site root)
                $parent=isset($parent) ? $parent : '0';
                
                // how many levels deep. i think it defaults to 10 levels if not set
                $depth=isset($depth) ? $depth : '';
                
                // get all children
                $childArray = $modx->getChildIds($parent, $depth);
                
                // get IDs from all children that are containers, published and not deleted
                $container = $modx->getDocuments($childArray, 1, 0, 'id', 'isfolder=1');
                
                // count the IDs
                $childrenCount = count($container); 
                
                // loop through the array if there are child containers
                 if ($childrenCount!==0){
                		for($x=0; $x<$childrenCount; $x++) {
                		$folderids .= $container[$x]['id'].",";
                		}
                		// within another Snippet use print, instead of return
                		print $folderids;
                	} else {
                	// return empty string if there are no containers
                	print '';
                	}
                
                ?>
                  • 3749
                  • 24,544 Posts
                  Quote from: sharkbait at Jun 06, 2010, 06:21 AM

                  Hi Bob,
                  thanks
                  return $folderids; 
                  is already returning a comma separated list.
                  btw. i found out, that if i use that Snippet within the other one ( link above) to return the parent IDs, i need to "print" the result instead of "return" smiley ...then it works

                  Duh. Of course it is.

                  About "print"ing the results. That’s weird, I would have expected the opposite.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 26931
                    • 2,314 Posts
                    About "print"ing the results. That’s weird, I would have expected the opposite.
                    afaik, "print" should be used, too if the Snippet is included as an external file. maybe there’s a connection.
                      • 4041
                      • 788 Posts
                      "print" should be used, too if the Snippet is included as an external file
                      FWIW,I like to use the following setup and it normally works pretty well.

                      Test snippet code
                      <?php
                      $output ='';
                      
                      include MODX_BASE_PATH.'assets/snippets/testsnippet/test_processor.php';
                      
                      return $output;
                      ?>


                      test_processor.php file contents
                      <?php
                      defined('IN_PARSER_MODE') or die('<b>UNAUTHORIZED_ACCESS_ERROR</b>');
                      
                      // add whatever you want to the $output
                      $output .='<p>Just a test message</p>';
                      ?>

                      :)
                        xforum
                        http://frsbuilders.net (under construction) forum for evolution