We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5811
    • 1,717 Posts
    Site-wide (or user or context) configuration should be done through settings
    But in this case these are only default configuration properties for a snippet. It’s not related to site-wide or user context huh
    Ok to examine the context settings solution to solve this problem, but, between us, I don’t find this very intuitive.

    And how could I set up programmatically a context for a component, in a different way than using the Contexts menu under System ? Which API should I use ? is there somewhere a piece of code as example ? Thanks.
      • 22303 MODX Staff
      • 10,725 Posts
      No, don’t create a Context for the component (it needs to be Context aware and agnostic); I’m suggesting adding system settings with a namespace of ajaxsearch. This is a standard way of making the system aware of a configurable option that spans multiple Snippets (i.e. one for rendering a search box, one for handling the AJAX requests, maybe others). AjaxSearch is more than just a single Snippet, so it is appropriate and almost every component we create that has more than a single element uses settings in this way.
        • 5811
        • 1,717 Posts
        Ok. I prefer. In the same time, I think I found the answer ... to retrieve the default properties, the getProperties for a snippet is enought.

        From Bob’s Guides - Revolution Objects (Thanks Bob !):
        You should be aware of the difference between Property Sets, and the Default Properties available on the Properties tab of elements. Those Default Properties are not technically a Property Set. In fact, they are the equivalent of a Property Set’s properties. The getProperties() method will get the properties of either one:

        /* get the Default Properties of a snippet (or other element) */
        $snippet = $modx->getObject(’modSnippet’,array(’name’=>’SnippetName’));
        $properties = $snippet->getProperties();

        /* Get the properties of a particular Property Set */
        $propSet = $modx->getObject(’modPropertySet’,array(’name’=>’PropertySetName’));
        $properties = $propSet->getProperties();

        So this should solve my problem smiley

        Ok for creating a system settings with a namespace of ajaxSearch.
          • 3749
          • 24,544 Posts
          If you do end up using properties, bear in mind that when you use getProperties() for a snippet, same-name properties in a custom property set specified in the snippet tag will override the default properties and same-name parameters in the snippet tag will override both of those.

          [Update] I meant to say $scriptProperties rather than getProperties(). $propertySet->getProperties() will retrieve a particular property set’s properties and it usually used internally by MODx.
            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
            • 22303 MODX Staff
            • 10,725 Posts
            I’m confused; getProperties is used internally; that is not really a function that needs to be called explicitly, ever.
              • 5811
              • 1,717 Posts
              I’m confused; getProperties is used internally; that is not really a function that needs to be called explicitly, ever.
              Ok but I am interesting to have an equivalent. Because I think this could be really usefull.

              If you do end up using properties, bear in mind that when you use getProperties() for a snippet, same-name properties in a custom property set specified in the snippet tag will override the default properties and same-name parameters in the snippet tag will override both of those.
              This is right for the $scriptProperties array, but ...

              I have modified a bit the doodle snippet to do some tests:
              1/ The doodle (beta 2) is delivered with a default properties set locked: tpl=’rowTpl’, sort=’name’, dir =’desc’. these values are provided with the build process thru the data/properties/properties.php.

              2/ I have set up a property set named "newTpl" with tpl=’newTpl’. This property set override the ’rowTpl’ default value.

              3/ I call Doodles like this : [[Doodles@newTpl? &sort=`description` &dir=`ASC`]]
              So the default tpl, sort and dir properties are overriden by the "newTpl" property set and by the properties passed thru the snippet call : &sort=`description` &dir=`ASC`

              4/ I have modified a bit the snippet script in order to display some values:
              /* show default properties */
              $snippet = $modx->getObject('modSnippet',array('name'=>'Doodles'));
              $properties = $snippet->getProperties();
              
              $output = '';
              foreach($properties as $key=>$value) {
                  $output .= "$key = $value, ";
              }
              echo "default properties: " . $output . "<br /><br />";
              
              /* show newTpl properties */
              $propSet = $modx->getObject('modPropertySet',array('name'=>'newTpl'));
              $newTplProperties = $propSet->getProperties();
              
              $output = '';
              foreach($newTplProperties as $key=>$value) {
                  $output .= "$key = $value, ";
              }
              echo "newTpl properties: " . $output . "<br /><br />";
              
              /* show final properties */
              $output = '';
              foreach($scriptProperties as $key=>$value) {
                  $output .= "$key = $value, ";
              }
              echo "script properties: " . $output . "<br /><br />";


              I get the following result result:
              default properties: tpl = rowTpl, sort = name, dir = DESC,
              
              newTpl properties: tpl = newTpl,
              
              script properties: tpl = newTpl, sort = description, dir = ASC,


              Which is interesting is that I correctly get the default property set by using:
              $snippet = $modx->getObject('modSnippet',array('name'=>'Doodles'));
              $properties = $snippet->getProperties();


              So Jason, does it possible to have in the futur a public non-internal method to retrieve these default values like the $snippet->getProperties() ?
                • 28215
                • 4,149 Posts
                His point is that you *shouldnt* ever have to grab the default properties. You should *always* grab the properties that are provided in $scriptProperties.

                Bypassing this means that your users can’t override things with tags, property sets or tag properties.

                If you have settings that you want to go to an ajax handler, you need to either:

                a) Pass them over ajax.
                b) Make them Settings.
                  shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                  • 5811
                  • 1,717 Posts
                  If you have settings that you want to go to an ajax handler, you need to either:

                  a) Pass them over ajax.
                  b) Make them Settings.
                  I totally agree with you. But I would like pass ONLY the custom properties (Not all the properties of the $scriptProperties array)
                  So this is the reason why I would like to retrieve the default properties that are passed thru the build process.

                  Except by using this getProperties() method, I don’t know how I could get these default properties.


                  Bypassing this means that your users can’t override things with tags, property sets or tag properties.
                  No. Because I do an array difference between the $scriptProperties array passed thru the snippet call and the default properties. So the ajax part receive all the tags customised by the user. I keep safe all the properties.
                    • 3749
                    • 24,544 Posts
                    Quote from: OpenGeek at Apr 23, 2010, 07:33 AM

                    I’m confused; getProperties is used internally; that is not really a function that needs to be called explicitly, ever.

                    I meant to say $scriptProperties (I updated the post above). But how would you do this without getProperties()?

                    $set = $modx->getObject('modPropertySet',array('name'=>'MyCustomPropertySet'))
                    $props = $set->getProperties();
                    $output .= $modx->getChunk('PropChunk', $props);
                    
                      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
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Hold on guys. Property Sets are applied through tags and are not intended to be used programatically in a snippet like that to get data for a Chunk tpl. TBH, I don’t see a scenario where using that is in any way beneficial. In fact, doing things that way could potentially create a mess of unnecessary dependencies, IMO. How does anyone know what Property Set you are hardcoding into your Snippet? How is that relationship maintained? If you’re not hardcoding it, then why not just call the property set in the tag as intended and simply pass the scriptProperties to the chunk?