Subscribe: RSS
  • I'm on Modx Revolution 2.6.0-pl, PHP 5.5.38, MySQL 5.6.23.

    I have a snippet that gets a list of all resources on the site (with some filtering) for use in a single-select TV. Everything works fine until I also try to get the parent page titles of all of those resources to help make selecting the correct page a little easier as some of my pages have identical pagetitles.

    Below is the snippet that works:
    <?php
    $excludeParents = array(42,2,41,31,25,22);
    $c = $modx->newQuery('modResource');
    $c->where(array(
        'modResource.parent:NOT IN' => $excludeParents,
        'modResource.id:NOT IN' => $excludeParents
    ));
    $resources = $modx->getCollection('modResource',$c);
    
    $pageList = [];
    foreach ($resources as $resource) {
        $pageList[] = $resource->get('pagetitle') . '==' . $resource->get('id');
    }
    
    $output = implode("||",$pageList);
    
    return $output;


    Below is one of my attempts at getting the parent page titles, this results in a 500 error with no errors being logged in Modx or in server error_logs that I can find.
    <?php
    $excludeParents = array(42,2,41,31,25,22);
    $c = $modx->newQuery('modResource');
    $c->where(array(
        'modResource.parent:NOT IN' => $excludeParents,
        'modResource.id:NOT IN' => $excludeParents
    ));
    $resources = $modx->getCollection('modResource',$c);
    
    $pageList = [];
    foreach ($resources as $resource) {
        $parentID = $resource->get('parent');
        $parent = $modx->getObject('modResource', $parentID);
        
        $pageList[] = $resource->get('pagetitle') . ' ('. $parent->get('pagetitle') .')==' . $resource->get('id');
    }
    
    $output = implode("||",$pageList);
    
    return $output;

    If I just use $parentID in place of $parent->get('pagetitle'), it shows the parent ID's.

    I thought maybe Modx didn't like trying to get page titles for ID's of 0, so I also tried adding an if statement to only try to get parent titles if the ID wasn't 0. However, this still results in a 500 error.
    <?php
    $excludeParents = array(42,2,41,31,25,22);
    $c = $modx->newQuery('modResource');
    $c->where(array(
        'modResource.parent:NOT IN' => $excludeParents,
        'modResource.id:NOT IN' => $excludeParents
    ));
    $resources = $modx->getCollection('modResource',$c);
    
    $pageList = [];
    foreach ($resources as $resource) {
        $parentID = $resource->get('parent');
        
        $parentTitle = '';
        if($parentID !== '0'){
          $parent = $modx->getObject('modResource', $parentID);
          $parentTitle = $parent->get('pagetitle');
        }
        
        $pageList[] = $resource->get('pagetitle') . ' ('. $parentTitle .')==' . $resource->get('id');
    }
    
    $output = implode("||",$pageList);
    
    return $output;


    I appreciate any help!
    • This doesn't work in newer version of PHP:

      $pageList = [];


      It should be:

      $pagelist = array();


      FWIW, there's nothing like a good code editor. I pasted your snippet into PhpStorm and the [] showed up in red with a hover message about why it was a problem. I just solved a 500 error problem of my own last night by doing the same thing.

      It seems to me that PHP 7 is much more likely to throw Server 500 errors when facing simple PHP syntax or usage errors, so having a code editor that does good diagnostics is even more critical now.
        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
      • If the server is running PHP 5.5.38, I'm guessing that wouldn't be an issue? I'll try to look into something better for coding PHP, though, and maybe switch the server over to PHP 7.

        In the mean time, using the following still throws a 500 server error:
        <?php
        $excludeParents = array(42,2,41,31,25,22);
        $c = $modx->newQuery('modResource');
        $c->where(array(
            'modResource.parent:NOT IN' => $excludeParents,
            'modResource.id:NOT IN' => $excludeParents
        ));
        $resources = $modx->getCollection('modResource',$c);
         
        $pageList = array();
        foreach ($resources as $resource) {
            $parentID = $resource->get('parent');
            $parent = $modx->getObject('modResource', $parentID);
             
            $pageList[] = $resource->get('pagetitle') . ' ('. $parent->get('pagetitle') .')==' . $resource->get('id');
        }
         
        $output = implode("||",$pageList);
         
        return $output;
        • Sanity checks are much more important in Php7. It seems to throw 500 errors for many PHP errors.

          You're not checking the return values for the two getObject() calls.

          If a resource is at the root of the tree, it has no parent object ($parentID will be 0), so $parent->get('pagetitle') throws an error: "Fatal error: Call to a member function get() on null".
            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
          • W. Shawn Wilkerson Reply #5, 8 months, 2 weeks ago
            Quote from: firebot6 at Jan 18, 2018, 11:16 AM
            I'm on Modx Revolution 2.6.0-pl, PHP 5.5.38, MySQL 5.6.23.

            I have a snippet that gets a list of all resources on the site ....
            I appreciate any help!

            Why not use pdoTools:
            https://www.shawnwilkerson.com/site-info/sitemap.html (instructions at the bottom). Essentially, you just have to template the output.
              Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

              Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com