We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39932
    • 483 Posts
    I'd like to know if ModX has the capability to utilize the awesome power of PropertySets from a snippet or plugin. I'm looking for a solution that will run snippets of different PropertySets dynamically. I know I can use
    $modx->runSnippet())
    , but this doesn't choose the PropertySet for you and just dumps the array provided into the
    $scriptProperties[]
    .

    I'm guessing that this requires parsing the properties of a given Set into an array? If so, how? Is there an easier solution?

    I've pored through the documentation, read Mr. BobRay's site, but I just cannot seem to find a straight-forward answer. Maybe I'm missing something?

    This question has been answered by BobRay. See the first response.

      Website: Extended Dialog Development Blog: on Extended Dialog
      Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
      Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

      Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
    • discuss.answer
      • 3749
      • 24,544 Posts
      I'm doing this off the top of my head, so it may be wrong. This assumes that the snippet called with runSnippet() has default properties and that your snippet has properties (either default or in the tag), so you may not need all of it:

      <?php
      
      /* get the default properties of the snippet being run with runSnippet() */
      $snippet = $modx->getObject('modSnippet', array ('snippet' => 'SnippetName'));
      $props = $snippet->getProperties();
      
      /* get the properties from the property set */
      $propertySet = $modx->getObject('modPropertySet', array ('name' => 'propertySetName'));
      $properties = $propertySet->getProperties();
      
      /* combine those two. The property set properties will take precedence */
      $props = array_merge($props, $properties);
      
      /* add in the properties from your snippet (these will take precedence) */
      $props = array_merge($props, $scriptProperties);
      
      /* call runSnippet with the merged properties */
      $modx->runSnippet('SnippetName', $props);



      Note that the property set will contain only properties that are different from the snippet's default properties, so any properties not overridden in the property set will be missing from the property set's properties -- hence the need to merge in the snippet's default properties. The code assumes that you want properties set for your own snippet to take precedence over same-name properties in the called snippet or property set. If that's not the case, you can reverse the arguments in array_merge() -- array members in the right-hand arg overwrite ones in the left-hand arg.

      The code would be slightly more efficient if compressed to a couple of lines, but would be much harder to read.



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 3 months ago.]
        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
        • 39932
        • 483 Posts
        Thanks!! I'm going to give it a try, but it looks like it will do it. For the life of me, I could not find
        getProperties()
        ! Sure enough, I looked in the API for the 4th dozen time and there it was! I blame it on my exhaustive use of
        getOne()
        and
        getMany()
        . (P.S. Those functions are simply too handy.

        Time to be more diligent!! Thanks again. It will work like a charm, I'm sure.

          Website: Extended Dialog Development Blog: on Extended Dialog
          Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
          Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

          Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".
          • 22303 MODX Staff
          • 10,725 Posts
          Out of curiosity, why not use the parsing engine to set the property set in the tag dynamically, e.g.
          [[MySnippet@[[!selectPropertySet]]? &override=`overridden`]]

          Just isolate your logic for selecting the appropriate property set in the selectPropertySet snippet. All it needs to do is return the name of the property set...

          If that doesn't do what you want, look into using modParser->getElement, which does allow you to use a property set in the element name (since 2.1.0-rc3), like so...
          $mySnippet = $modx->parser->getElement('modSnippet', "MySnippet@{$propertySet}");
          return $mySnippet->process($scriptProperties);
            • 39932
            • 483 Posts
            @Opengeek:

            Simply because I was not aware of how to perform that task (i.e. did not know that you could call getElement with an @). Now that I know that, it's even easier. I've already found a way to use BobRay's solution.

            Are there any run-time differences between BobRay's and yours? (Aside from an unnoticable difference in performance, that is?)
              Website: Extended Dialog Development Blog: on Extended Dialog
              Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
              Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

              Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".