We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34147
    • 28 Posts
    I am making a snippet but I need a way to debug and test. How does everyone else do this. I am posting it here for people to look over. I also don't know how I am supposed to handle the &tpl like getResources does.

    <?php
    
    /* set parameters */
    $parent = (!empty($parent) || $parent === '0');
    $depth = isset($depth) ? (integer) $depth : 1;
    $tpl = isset($tpl) ? $tpl : 'chunkname';
    
    /* get child ids of parent */
    $categories = $modx->getChildIds($parent, $depth);
    
    /* new query */
    $criteria = $modx->newQuery('modResource');
    
    /* retrieve only published, hidden, and isContainer*/
    $criteria->where(array(
    	'published' => true,
    	'deleted' => false,
    	'hidemenu' => true,
    	'isfolder' => true,
        'id:IN' => $categories,
    ));
    
    $criteria->sortby('pagetitle','DESC');
    
    /* get the resources as xPDOObjects */
    $categories = $modx->getCollection('modResource',$criteria);
    
    /*  output */
    $output = array();
    foreach ($categories as $category) {
        $phs = array_merge(
           array('count' => $category->hasChildren()),
           $category->toArray()
        );
        $output[] = $modx->getChunk('tpl',$phs);
    }
    $output = implode("\n",$output);
    return $output;
    
    ?>
      • 33983 ☆ A M B ☆
      • 181 Posts
      I would use the getOption method:
      $debug = (boolean) $modx->getOption('debug', $scriptProperties, false);
      

      Then you can set in your snippet call like this [ [mysnippet? &debug=`1`] ].

      If debug then you can log it to the MODX error log like this:
      $modx->log(modX::LOG_LEVEL_ERROR, '[mysnippet] Some error message');
      

      Or just output your error or messages.

      As for the chunk, the 'tpl' is the name of the chunk and I would recommend using getOption so you can change the chunk for each call to the snippet. So like this:
      $myChunk = $modx->getOption('tpl', $scriptProperties, 'tpl');
      ...
      $output[] = $modx->getChunk($myChunk,$phs);
      

      And then you could call on this like [ [mysnippet? &tpl=`someChunkName`] ]
      And remember what ever chunk you call on needs to have [[+field]] in it to display the data.

      Hopefully that makes sense and helps.
        • 34147
        • 28 Posts
        Great, thanks for the debug help. I will get on that. As for the tpl, I knew I would need to make an option for it, my concern was the fall back if there is no tpl defined in the call. Am I suppose to package one in, and if so, how?
          • 34147
          • 28 Posts
          Great, thanks for the debug help. I will get on that. As for the tpl, I knew I would need to make an option for it, my concern was the fall back if there is no tpl defined in the call. Am I suppose to package one in, and if so, how?
            • 33983 ☆ A M B ☆
            • 181 Posts
            You can reveiw the docs: http://rtfm.modx.com/display/revolution20/Templating+Your+Snippets#TemplatingYourSnippets-TemplatingtheSnippet.

            Also look at the getOption explained in: http://rtfm.modx.com/display/revolution20/Developing+an+Extra+in+MODX+Revolution#DevelopinganExtrainMODXRevolution-CreatingtheDoodlesSnippet.

            Basically the 3 params are:
            //  $modx->getOption(Key/Name, Array or Null, Default Value);
            $myChunk = $modx->getOption('tpl', $scriptProperties, 'tpl');
            

            getOption will then return the default value if the Key is not set.
              • 34147
              • 28 Posts
              Alright so I have been working on this a little more and have revised some of the lines, but I am running into trouble figiuring out how get more detailed errors. I started by droping in a few "or die()" statements which helped at least.

              So here is the updated code:
              <?php
              
              /* set parameters */
              $parent = (!empty($parent) || $parent === '0');
              $depth = isset($depth) ? (integer) $depth : 1;
              $tpl = isset($tpl) ? $tpl : 'tpl';
              $debug = (boolean) $modx->getOption('debug', $scriptProperties, false);
              
              
              /* new query */
              $criteria = $modx->newQuery('modResource');
              
              /* retrieve only published, hidden, and isContainer*/
              $criteria->where(array(
              	'published' => true,
              	'deleted' => false,
              	'hidemenu' => true,
              	'isfolder' => true,
              ));
              
              /* get child ids of parent */
              $categories = $modx->getChildIds($parent, $depth);
              if(count($categories) > 0) {
              	$criteria->where(array(
              		'id:IN' => $categories,
              		)) or die($modx->log(modX::LOG_LEVEL_ERROR, '[getCategories] insert id Failed'));
              	}
              
              $criteria->sortby('pagetitle','DESC');
              
              /* get the resources as xPDOObjects */
              $categories = $modx->getCollection('modResource',$criteria);
              
              /*  output */
              $output = array();
              foreach ($categories as $category) {
                  $phs = array_merge(
                     array('count' => $category->hasChildren()),
                     $category->toArray()
                  );
                  $output[] = $modx->getChunk('tpl',$phs);
              }
              $output = implode("\n",$output);
              return $output;
              
              ?>



              But now I am getting an error I don't understand at the getChildIds() part.
              `+id is not a valid integer and may not be passed to makeUrl()

              I am not sure why it says that.