We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15566
    • 73 Posts
    I have coding an xml document which returns the structure of my MODx (Revo) installation using the following code. The problem is that getChildIds returns resources over multiple levels but flattens them into a single level array. Is there an alternative request to maintain the hierarchy?

    echo '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
    <sections>';
    
    $docs = $modx->getCollection('modResource', array(
        'id:IN' => $modx->getChildIds(2), // (start, depth[optional])
        'hidemenu:!=' => '1', // only show menu visible
        'isfolder:=' => '1' // only show folders
    ));
    
    foreach ($docs as $doc) {
        echo '<section>';
        echo '<id>'.$doc->get('id').'</id>';
        echo '<name>'.$doc->get('pagetitle').'</name>';
        echo '</section>';
    }
    
    echo '</sections>';
      • 17403 ☆ A M B ☆
      • 183 Posts
      I use "uri" to maintain the hierarchy. Add ORDER BY `uri` to the query, then loop through the results to get the hierarchy.
        zaenal.lokamaya
        • 3186 ☆ A M B ☆
        • 279 Posts
        A workaround could be to use GoogleSiteMap component/snippet? or maybe use Wayfinder?
        You can easily change the chunks to markup your own structure.
          MODX Ambassador (NL) & Professional MODX developer
          Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
          MODX e-commerce solution SimpleCart
          • 15566
          • 73 Posts
          Thanks guys. It did cross my mind to use either an existing Sitemap or Wayfinder snippet, but I wanted to be able to create something clean and simple by hand. Order by uri didn't seem to make any difference either. But as today is a new day and my brain is running on full steam I have come up with this, which seems to do the trick:

          function listSections($id){
          	global $modx;
          	$c = $modx->newQuery('modResource');
          	$c->where(array(
          	   'parent' => $id,
          	   'hidemenu:!=' => '1', // only show menu visible
          	   'isfolder:=' => '1' // only show folders
          	));
          	
          	$c->sortby('menuindex','ASC');
          	$docs = $modx->getCollection('modResource',$c);
          
          	foreach ($docs as $doc) {
          		$section .= '<section>';
          		$section .= '<id>'.$doc->get('id').'</id>';
          		$section .= '<name>'.$doc->get('pagetitle').'</name>';
          		$section .= listSections($doc->get('id')) ? '<subsections>'.listSections($doc->get('id')).'</subsections>' : '';
          		$section .= '</section>';	
          	}
          	
          	return $section;
          }
          
          echo '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
          <sections>'.listSections(2).'</sections>';


          By using a function I can call it on each node to find more children if they exist.