We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Is there an xPDO/MODx method to get a list of which resource groups the current resource belongs to? Imagine a site with multiple folders, each folder corresponds loosely (not 100% of the time) to a resource group. I want a couple Snippets inside those sub-folders to "know" which resource group they belong to. It involves tracking stuff in the database, so it’s important that I know where my form was submitted.

    I even thought about using a template variable and the @INHERIT method so I could specify the value on the parent folder... but I wanted to see if it might be easier to read the group directly.

    Thanks!
      • 26903
      • 1,336 Posts
      This is how the getlist resource group processor does it :-
      $c = $modx->newQuery('modResourceGroup');
      $c->leftJoin('modResourceGroupResource','ResourceGroupResource','
          `ResourceGroupResource`.`document_group` = `modResourceGroup`.`id`
      AND `ResourceGroupResource`.`document` = '.$resource->get('id'));
      $count = $modx->getCount('modResourceGroup',$c);
      $c->select('
          `modResourceGroup`.*,
          IF(ISNULL(`ResourceGroupResource`.`document`),0,1) AS access
      ');
      $c->sortby($sort,$dir);
      if ($isLimit) $c->limit($limit,$start);
      $resourceGroups = $modx->getCollection('modResourceGroup',$c);


      Maybe you could just call this processor.

        Use MODx, or the cat gets it!
      • How about adding a page to a certain Resource Group programmatically?
          • 8643
          • 271 Posts
          Something like this:
           //add resouce to resource group
          //$resouceID - resouce ID
          //$resourceGroupID - resource group ID
          
              $resourceGroupResource = $modx->newObject('modResourceGroupResource');
              $resourceGroupResource->set('document', $resouceID);
              $resourceGroupResource->set('document_group', $resourceGroupID);
              $resourceGroupResource->save();
            • 3749
            • 24,544 Posts
            Quote from: Everett at Jan 21, 2011, 11:44 PM

            How about adding a page to a certain Resource Group programmatically?

            elastic’s method should work fine (and would be a little faster), but there’s an easier way now:

            $resource->joinGroup($group);


            $group can be a resource group object, the ID of the resource group, or the name of the resource group (using the ID would be fastest).

            I’m shooting from the hip here, but I think if you just want the user group IDs in an array, this would do it:

            <?php
            $id = $modx->resource->get('id');
            $rgrs = $modx->getCollection('modResourceGroupResource', array('document'=>$id));
            
            $groups = array();
            foreach($rgrs as $rgr) {
                $groups[] = $rgr->get('document_group');
            }
            
            /* see if we got them */
            $output = implode(',', $groups);
            return $output;
            
              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
              • 38290
              • 712 Posts
              What about removing a resource from one (or all) resource groups? I'm trying to write a script to, after duplicating a context say go through this context, change all resources with this resource group to that resource group [ed. note: dinocorn last edited this post 10 years, 10 months ago.]
                jpdevries
                • 3749
                • 24,544 Posts
                This will remove a resource from a resource group:

                $resource->leaveGroup($group);
                  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
                  • 38290
                  • 712 Posts
                  Thanks Bob! I'll get back to seeing what I can do with this script.
                    jpdevries
                    • 40045
                    • 534 Posts
                    With this PR https://github.com/modxcms/revolution/pull/11364 you could simply do:

                    $rgrouplist = $resource->getGroupsList();
                    $rgroups = array();
                    
                    foreach ($rgroupslist['collection'] as $rgroup) {
                        if ($resource->isMember($rgroup->get('name'))) {
                            // do something
                        }
                    }