We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33659
    • 5 Posts
    Hallo ModX-Community,

    after updating from revolution 2.0.8 to 2.1.1 I get an error for a self created snippet (snippet name: "getChild"):

    Fatal error: Call to undefined method modX::getAllChildren() in /core/cache/includes/elements/modsnippet/17.include.cache.php on line 10



    This is my "getChild" snippet:

    <?php
    $id = $modx->runSnippet("UltimateParent");

    if ($id == 4 || $id == 6 ) {
    $children= $modx->getAllChildren($id, ’menuindex’, ’ASC’, ’pagetitle, parent, alias, content, id’);
    if (!$children === false) {
    foreach($children as $child)
    {
    $output .= ’:or:is=`’.$child[’id’].’`’;
    }
    }
    return $output;
    }


    After some search I have found at the "Summary of Legacy Code Removed in 2.1" http://rtfm.modx.com/display/revolution20/Summary+of+Legacy+Code+Removed+in+2.1 that the Method: modX->getAllChildren() is removed in Revolution 2.1. The replacement or potential workaround is: modX->getCollection(’modResource’, $criteria) , $criteria having where condition ’parent’ = $id

    I have created the "getChild" snippet to load the Javascript for a slideshow only at ID=4 or ID=6.

    Here is the chunk (named: Scripts), which implements the JS-Code to the header:

    [[*id:is=`1`[[getChild?]]:then=`<script src="js/jquery.1.4.2.min.js" type="text/javascript"></script>
    <script src="js/jquery.cycle.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    [[*id:is=`1`:then=`[[$CycleSlideshow]]`:else=`[[$CycleImageViewer]]`]]
    </script>`:else=``]]


    But now to my question: How can I change my snippet with the replaced method?
    Or is there a better solution to load the Slideshow-Javascript only at id= 4 and 6?

    Many thanks in advance!

    best regards
    flo
      • 24068
      • 4 Posts
      Quote from: zuttec at Jun 17, 2011, 09:00 AM

      $output .= ’:or:is=`’.$child[’id’].’`’;
      Quote from: zuttec at Jun 17, 2011, 09:00 AM

      [[*id:is=`1`[[getChild?]]:then=`<script src="js/jquery.1.4.2.min.js" type="text/javascript"></script>
      <script src="js/jquery.cycle.min.js" type="text/javascript"></script>
      <script type="text/javascript">
      [[*id:is=`1`:then=`[[$CycleSlideshow]]`:else=`[[$CycleImageViewer]]`]]
      </script>`:else=``]]


      Wow, this does realy work?

      Quote from: zuttec at Jun 17, 2011, 09:00 AM

      But now to my question:
      Quote from: zuttec at Jun 17, 2011, 09:00 AM

      $children= $modx->getAllChildren($id, ’menuindex’, ’ASC’, ’pagetitle, parent, alias, content, id’);
      Quote from: zuttec at Jun 17, 2011, 09:00 AM

      How can I change my snippet with the replaced method?

      Basicly with

      $children= $modx->getCollection('modResource', array('parent'=>$id));


      But sorting by ’menuindex’, ’ASC’ is missing. Have a look at xpdo-queries:

      $query = $modx->newQuery('modResource');
      $query->where(array(
        'parent' => $id
      ));
      $query->sortby('menuindex', 'ASC');
      $children= $modx->getCollection('modResource', $query);
      


      http://rtfm.modx.com/display/xPDO20/xPDOQuery

      Quote from: zuttec at Jun 17, 2011, 09:00 AM

      Or is there a better solution to load the Slideshow-Javascript only at id= 4 and 6?

      Anyone?
        • 24068
        • 4 Posts

        <?php
        $output = '';
        
        $id = $modx->runSnippet("UltimateParent");
        if ($id == 4 || $id == 6 ) {
          $query = $modx->newQuery('modResource');
          $query->where(array(
            'parent' => $id
          ));
          $query->sortby('menuindex', 'ASC');
          $children= $modx->getCollection('modResource', $query);
          if (!empty($children)) {
            foreach($children as $child) {
              $output .= ':or:is=`'.$child->get('id').'`';
            }
          }
        }
        return $output;
        
          • 33659
          • 5 Posts
          Wow... Thank You very much! nice, it solved my problem ...and sorry for my bad modX based php skills laugh
            • 22303 MODX Staff
            • 10,725 Posts
            As a side note, you can now use $modx->getIterator() in place of getCollection() in code like this, e.g.

            <?php
              $children= $modx->getIterator('modResource', $query);
              if (!empty($children)) {
                foreach($children as $child) {
                  $output .= ':or:is=`'.$child->get('id').'`';
                }
              }
              • 33659
              • 5 Posts
              I tested both of these codes in the snippet and they work great. I will use now the getIterator query syntax.

              <?php
              $id = $modx->runSnippet("UltimateParent");

              if ($id == 4 || $id == 6 ) {
              $children= $modx->getIterator(’modResource’, $query);
              if (!empty($children)) {
              foreach($children as $child) {
              $output .= ’:or:is=`’.$child->get(’id’).’`’;
              }
              }
              return $output;
              }



              Thank you again...you guys are awesome:)