Subscribe: RSS
  • Hi,
    In a Custom Manager Page I am creating a new resource and want to a) create a new resource group and b) add the new resource to that resource group.
    The code I am using for creating the resource itself is similar to the following:
    $page = $this->modx->newObject('modDocument');
    
    $pagedata = array( PAGEDATA HERE );
    
    $page->fromArray($pagedata);
    
    $page->save();
    
    $newpageid = $page->get('id');
    
    


    Can anyone give me any pointers for the methods to use: I guess I’m looking for something like "newObject(’modResourceGroup’)", then setResourceGroup($newpageid,$newgroupid) (Although obviously that would be a bit too easy!).

    Thanks for any help in advance.
    • OK, so this seems to work for creating a new group:
      $group = $this->modx->newObject('modResourceGroup');
      //Not sure about hardcoding the private_memgroup and _webgroup...
      $groupdata = array('name'=>'MYNAME,'private_memgroup'=>0,'private_webgroup'=>0);
      $group->fromArray($groupdata);
      $group->save();
      $newgroupid = $group->get('id');
      

      But the question remains about how to assign a resource to it using the api.

      Nothing like a discussion with yourself...
      • I guess writing things in a forum is the magic solution to finding what you’ve been looking for over the last frustrating coding hours...

        This seems to work, unless anyone can suggest a reason why it’s bad:

        $assign = $modx->newObject('modResourceGroupResource');
        $data = array('document_group'=>MYGROUPID,'document'=>MYDOCID);
        $assign->fromArray($data);
        $assign->save();
        
        • $resource->joinGroup('ResourceGroupName');
          /* or */
          $resource->joinGroup(123); /* id of resource group */
          
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • With this PR https://github.com/modxcms/revolution/pull/11364 error log spam could be prevented like this (as the join/leaveGroup() methods throw an error each time a resource is already in a group or not in a group...)

            if (!$resource->isMember('ResourceGroupName')) {
                $resource->joinGroup('ResourceGroupName');
            }