We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10378
    • 375 Posts
    Reading a setting is easy as MODX merges system, context (and user settings) at runtime.
    So using this line of code you will get a value first from user settings overriding context settings overriding system settings (in this order). You don't have to know which one exists.

    $setting = $modx->getOption('my_setting');


    My question is: Is there also an easy method to change a settings value in reverse order without knowing if it exists in context or system?

    Thanks,
    Martin


      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 3749
      • 24,544 Posts
      There's some info here that might help: http://bobsguides.com/blog.html/2013/06/19/displaying-modx-context-settings/.

      It depends on what you mean by easy. wink


      This should get you started:


      <?php
      $key = 'my_setting';
      
      if (! isset($scriptProperties[$key])) {
         /* none of them exist */
      } 
      
      $csObj = $modx->getObject('modContextSetting',
              array(
                  'context_key' => 'web', // or whatever
                  'key' => $key,
              )
      };
      
      $ssObj = $modx->getObject('modSystemSetting',
             
              array(
                  'key' => $key,
              )
      };
      
      /* Not sure about this one */
      
      $props = array();
      $snippet = 'mySnippet';
      $snippetObj = $modx->getObject('modSnippet', array('name' => $snippet));
      
      if ($snippetObj) {
          $props = $snippet->getProperties();
      }
      
      if (! empty($props) {
          if (in_array($key, $props) {
              $value = $props[$key]
          } else {
              /* no property */
          }
      }
      
      
      if ($csObject) {
         $value = $csObj->get('value');
      } else {
         /* no context setting */
      }
      
      
      if ($ssObject) {
         $value = $ssObj->get('value');
      } else {
         /* no System Setting */
      }
      


      Remember that any of these can exist but be empty.

        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
        • 10378
        • 375 Posts
        Hi Bob,

        as always very helpful information! This is what I needed. I'll adapt it to work in my project!

        Thanks,
        Martin
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/