We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    Working on a plugin that will assign a particular resource group to a resource when the document is saved. Not quite sure what the API call would look like.


    Would I use $modx->resource->addOne()?

    What is the name of the object I want to add? ResourceGroupResource? (Just Guessing). Is this documented somewhere?


      • 28215
      • 4,149 Posts
      There’s a nice API method for you. tongue
      $resource->joinGroup('IDorNameOfResourceGroup');



        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 14883 ☆ A M B ☆
        • 450 Posts
        Awesome!

        Works great except for one small string/int issue having to do with the way I’m getting the value I’m passing:
            // gets ID of ultimate parent resource
            $site = $modx->runSnippet('getSite', array('pid'=>$id));
            
            // gets "primary resource group" TV for the site
            $rgTV = $modx->getObject('modTemplateVar', array('name'=>'primaryResourceGroup'));
            $prg =  $rgTV->getValue($site);
        
            $resource->joinGroup($prg);
        


        This is failing, because even though my TV holds an integer, it is passing it as a string. I can get it to work by casting:
         $prg = (int) $rgTV->getValue($site);


        Or it would also work if the joinGroup method used is_numeric() rather than is_int(). Is that a change that could potentially be made? Or does it need to be is_int().

          • 28215
          • 4,149 Posts
          Quote from: jrotering at Mar 08, 2011, 03:53 PM

          Or it would also work if the joinGroup method used is_numeric() rather than is_int(). Is that a change that could potentially be made? Or does it need to be is_int().

          I think is_numeric could definitely work there. Can you file a bugfix for it?
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: splittingred at Mar 08, 2011, 04:21 PM

            Quote from: jrotering at Mar 08, 2011, 03:53 PM

            Or it would also work if the joinGroup method used is_numeric() rather than is_int(). Is that a change that could potentially be made? Or does it need to be is_int().

            I think is_numeric could definitely work there. Can you file a bugfix for it?
            Actually, is_numeric will not work there, as a Resource Group named 1.2 would be treated as the wrong type of search. The string "1.2" would be a valid Resource Group name.
              • 14883 ☆ A M B ☆
              • 450 Posts
              How about changing the function such that to be considered as an ID the input must 1) pass is_integer() and 2) contain only digits?

              I hesitate because I know that starts to make the function a bit convoluted.

              But on the other hand from an end-user perspective it was rather baffling that my code, which I knew was passing (what to me was for all intents and purposes) an integer, was not working properly.

              I think the options are

              1) leave it as it is and focus on educating end users that if they pass an ID, they have to make sure that it is of type ’integer’ and not a numeric string.
              2) change the function as I suggested above - the arg will be considered an ID if it passes is_integer and contains only digits.
              3) change the function so that it is ’less flexible’ - make it require an array for input "$resource->joinGroup(array(’id’=>$id))" or "$resource->joinGroup(array(’name’=>$name))"

              I think #2 and #3 are better alternatives than #1. I’ll file this as a bug, and leave it up to you core folks to decide what the "right" solution is.