We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33277
    • 35 Posts
    Dear MODx programmers,

    I'm trying to add a '&hierarchical' option to the GalleryAlbums snippet and am almost ready, but run into a problem I can't solve myself.

    I have a recursive function that gets an album object as array and outputs a <ul>-based tree (unsing chunks) for that resource. My problem is that it can't access some important variables outside of itself; declaring them as global doesn't help:
      function process_children ($album) {
        global $modx;
        global $rowTpl; // These two don't work.
        global $wrapperTpl; // ^^^
        global $outputSeparator;
        // global $albumChildren; // This one is visible though...
        $tree = '';
        if ($albumChildren[$album['id']]) {
          $modx->log(modX::LOG_LEVEL_ERROR, "Found child");
          foreach ($albumChildren[$album['id']] as $descendants) {
            $tree .= process_children ($descendants) . $outputSeparator;
          }
          return $modx->getChunk($wrapperTpl, array('rows' => $tree));
        } else {
          $modx->log(modX::LOG_LEVEL_ERROR, "Found row, $rowTpl");
          $tree = $modx->getChunk($rowTpl, $album) . $outputSeparator;
          return $tree;
        }
      }
    


    Please also note that the function declaration is inside such block:
    if ($hierarchical) {
      ..
    }
    
      <em>The Earth is not the hell, and people aren't cursed, and joy in not a sin, but a gift of God.</em> (Lesia Ukrainka)