We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34926
    • 87 Posts
    I am writing a plugin and am trying to change context settings on the fly. Under the system event 'OnWebPagePrerender' I have this:

    $newcontext = $modx->getObject('modContextSetting', array(
    							'context_key' => $modx->context->key,
    							'key' => $context
    						));		
    						$newcontext->set('value', $value);
    						//$newcontext->save();


    The save method works fine, as expected. I would like it to change temporarily just for the pages the plugin is active on. Is this possible?

    If not would saving the context and then saving it again (back to the original value) after the page has been rendered by inefficient?

    Cheers!
      • 33968
      • 863 Posts
      I think what you've done should work (without the save() method), however as I found out Context settings are cached so it's likely not to have any effect on the current page. I've filed a feature request somewhere along these lines. I also think there was a setting for cache_context_settings but can't remember if it worked or not, so try disabling that.

      Otherwise, another technique might be to use setPlaceholder to send your settings to standard placeholders instead. You could have them default to the existing Context Setting but simply override them on those specific pages you want to change the setting for.
        • 34926
        • 87 Posts
        Thanks for the info Lucas. I have all cache (including the context settings) disabled and it still doesn't appear to work for me. Is it possible that another system event at a lower level would have a different effect maybe?

        I will have a play and give your placeholder suggestion a go. If all else fails I can force a regular expression on the document output but would prefer to avoid that.

        Cheers
          • 3749
          • 24,544 Posts
          If would try using $modx->setOption('key', 'value'). That's supposed to make a temporary change to settings that only persists during the current request.

          Since system, context, and user settings are all merged into a single array by the time they reach your code, that should override anything else that's been done.

          FYI, when it comes to settings, user settings override context settings, which override system settings.




          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/modx.html
            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
            • 34926
            • 87 Posts
            Cheers BobRay, I didn't know that and very useful!
              • 33968
              • 863 Posts
              How weird, I thought you had written setOption in your original code - don't know what I was looking at yesterday tongue

              Did that work for you?
                • 33968
                • 863 Posts
                How weird, I thought you had written setOption in your original code - don't know what I was looking at yesterday tongue

                Did that work for you?