We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    Got it! It returns an array of modResource objects! What a pain with no documentation!
    <?php
    $object = $modx->getObject('modResourceGroup',3);
    $pages = $object->getResources();
    $output = '';
    foreach($pages as $value) {
      $output .= $value->pagetitle . "<br>";
    }
    return $output;
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 3749
      • 24,544 Posts
      It's an array of resource objects, so you have to do something like:

      foreach($pages as $page) {
          echo '<br />' . $page->get('pagetitle);
      
      }
        Did I help you? Buy me a beer
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        So here's my crude little utility for finding resources that aren't in a group, and making it easy to edit them so they can be added to the group.
        // get an array of all resources - I suspect there already is one in memory somewhere
        $c = $modx->newQuery('modResource');
        $resources = $modx->getCollection('modResource');
        $allresources = array();
        foreach($resources as $resource) {
            $allresources[] = $resource->id;
        }
        
        // get an array of all resources in a given group
        $group = $modx->getOption('group', $scriptProperties, 'defaultGroup');
        $object = $modx->getObject('modResourceGroup',array('name'=>$group));
        $pages = $object->getResources();
        $testresources = array();
        foreach($pages as $value) {
          $testresources[] = $value->id;
        }
        
        // get an array of the resources not in the group
        $notingroup = array_diff($allresources,$testresources);
        
        // make links for editing them 
        foreach($notingroup as $key=>$value) {
            $output .= '#' . $value . ' - <a href="http://localhost/revo228/manager/?a=30&id=' . $value . '">Edit</a><br>';
        }
        
        return $output;
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 3749
          • 24,544 Posts
          A little inefficient since you're getting the resources in the group twice, but if it works. wink

          Did you try my snippet above that gets them via the modTemplateVarResource objects?
            Did I help you? Buy me a beer
            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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I have a question. Does $modx->getTree() go to the database, or work from an existing array in memory?
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Quote from: BobRay at Jul 31, 2013, 06:25 PM
              A little inefficient since you're getting the resources in the group twice, but if it works. wink

              Did you try my snippet above that gets them via the modTemplateVarResource objects?
              Hm. Has to be $modx->newQuery, not $xpdo->newQuery, and it's inGroup, not ingroup. That takes care of the errors, but it shows 0 all the time for !inGroup. How do you display the actual query that is being used?
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 3749
                • 24,544 Posts
                Just tested this and it's working perfectly for me (and very fast):

                Be sure to send the ID of the resource group, not the name, or the ID of a user group (and call the snippet uncached).



                <?php
                $groupId = $scriptProperties['groupId'];
                $inGroup = $scriptProperties['inGroup'];
                
                $query = $modx->newQuery('modResourceGroupResource');
                
                if ($inGroup) {
                    $query->where(array('document_group' => $groupId));
                } else {
                    $query->where(array('document_group:!=' => $groupId));
                }
                
                $rgrs = $modx->getCollection('modResourceGroupResource', $query);
                $count = count($rgrs);
                
                foreach ($rgrs as $rgr) {
                    /** @var $rgr modResourceGroupResource */
                    $doc = $rgr->getOne('Resource');
                
                    echo "<br />" . $doc->get('pagetitle');
                    /* Do something with $doc object */
                }
                
                echo '<br /><br />Did something with ' . $count . ' Resources';
                  Did I help you? Buy me a beer
                  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
                  • 4172
                  • 5,888 Posts
                  Quote from: sottwell at Jul 31, 2013, 04:33 PM


                  Not sure how to use the migx snippet; what should the tpl look like?

                  [[migxLoopCollection?
                  &classname = `modResource`
                  &joins=`[{"alias":"ResourceGroupResources"}]`
                  &where=`{"ResourceGroupResources.document_group":"25"}`
                  &tpl=`myTpl`
                  ]]


                  I think

                  <li>[[+pagetitle]]([[+id]])</li>


                  should work [ed. note: Bruno17 last edited this post 13 years, 2 months ago.]
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Quote from: BobRay at Jul 31, 2013, 10:39 PM
                    Just tested this and it's working perfectly for me (and very fast):

                    Be sure to send the ID of the resource group, not the name, or the ID of a user group (and call the snippet uncached).
                    Sorry, still comes up with 0 for &inGroup=`0` (or not using the property at all). I have four resources, one in the group, three not. This shows 1 just fine if I use &inGroup=`1`
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      <ul>
                      [[migxLoopCollection?
                      &classname = `modResource`
                      &joins=`[{"alias":"ResourceGroupResources"}]`
                      &where=`{"ResourceGroupResources.document_group":"3"`
                      &tpl=`@CODE:<li>[[+pagetitle]]([[+id]])</li>`
                      ]]
                      </ul>

                      Just lists all resources, whether in the group or not.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org