We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3677
    • 130 Posts
    I’m trying to figure out how I would go about setting context settings via a gateway plugin that resolves multiple hostnames to the right context.

    Currently I have something like this:
    <?php
    switch ($modx->getOption('http_host')) {
    
        case 'www.domain.com':
            $modx->switchContext('mycontext1');
            break;


    I’ve defined custom context settings on ’mycontext1’ through the manager edit context settings.
    Is it possible to add some code in my case statements that would over-ride and persist these custom context settings for the entire session?

    Thanks,
    -Brian
      • 3677
      • 130 Posts
      I found this information: http://rtfm.modx.com/display/revolution20/System+Settings

      $Setting = $modx->getObject('modSystemSetting', 'site_name');
      $Setting->set('value', 'My New Site Name');
      $Setting->save();


      However, "modSystemSetting" doesn’t seem to be the same thing as a context level setting. This will successfully change the site level system setting, but that doesn’t help me. Is there something similar that will change the context level setting?

      -Brian
        • 26903
        • 1,336 Posts
        You need the modContextSetting class, giving you key/value pairs for contexts, have a look at /core/model/modx/processors/context/setting/update.php for usage.
          Use MODx, or the cat gets it!
          • 3749
          • 24,544 Posts
          Is it possible to add some code in my case statements that would over-ride and persist these custom context settings for the entire session?

          I don’t think so. You can alter them for the duration of a *request*, or you can change them permanently in the DB. I could be wrong.
            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
            • 3677
            • 130 Posts
            Hey guys I found some details on modContextSetting and I’ve been able to modify the DB directly like this:

                  $Setting = $modx->getObject('modContextSetting', array(
                   'context_key' => 'MySiteContext',
                   'key' => 'my_context_setting',
                   ));	
                   $Setting->set('value', 'NewValue');
                   $Setting->save();


            So this looks promising. Like I said I am running a gateway plugin on handle request event to detect the hostname and set the context. I have a dozen or so of these context settings I want to set before switching the context in the plugin. I don’t really want to clutter the plugin code with a dozen of the above on every context.

            Is it not possible to call a Snippet from within a plugin? I was thinking I could call a snippet at the point where the context settings are setup and pass all of the setting fields as properties to the snippet. When I try that I get an error for an unexpected "[" as if a snippet call is not do-able inside a plugin. Is that the case?

            Thanks,
            -Brian
              • 26903
              • 1,336 Posts
              You could just include an external file into the plugin and put your code in that, this is the approach I adopt, from Frontpage for instance :-
              /* Create a Frontpage class */
              $corePath = $modx->getOption('frontpage.core_path',null,$modx->getOption('core_path').'components/frontpage/');
              include_once $corePath . "model/frontpage/frontpage.class.php";
              $fp = new Frontpage($modx);
              $fp->initialize($scriptProperties);
                Use MODx, or the cat gets it!
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                Indeed you can’t use [[snippetname]] in a plugin (or other snippet).

                Use $modx->runSnippet(’snippetname’,array(’option’ => ’value’)) instead.
                  Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                  Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                  • 3677
                  • 130 Posts
                  Thanks Mark.

                  I think I still have a fundamental problem if the context settings can not be persisted for only that visitor session.
                  My approach of changing the db value directly is going to be trouble because these are going to be overwritten every time the gateway resolves another host.
                  The values for the context settings need to be available throughout the site templates to dynamically change the content. What will happen is that the value of the setting will be different at any given moment based on the last visitor.

                  Uggg...

                  -Brian



                    • 28215
                    • 4,149 Posts
                    Have you tried just calling $modx->setOption(’setting_key’,’setting_value’) in your switcher plugin?
                      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                      • 3677
                      • 130 Posts
                      No, my understanding from other threads is that setOption is not persistent.
                      http://modxcms.com/forums/index.php?topic=64428.0

                      setOption is only a temporary change for the current request cycle (it does not change any values in database OR session).

                      -Brian