We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28215
    • 4,149 Posts
    Quote from: bmilton at May 28, 2011, 07:00 PM

    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).

    Right, but it is persistent for the request cycle - ie, that page load. And since your plugin fires on each page load...
      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
      • 3677
      • 130 Posts
      Hmmm....is it? Shows what I know. The gateway plugin uses the system event "OnHandleRequest"...so that is firing on every single page request?

      I’ll have to experiment with setOption.

      -Brian
        • 3677
        • 130 Posts
        Well, I can’t seem to get setOption to set a value at all. A few questions...

        1. Should it be sufficient to use switchContext and then immediately start setting options for that context?

            case 'www.mysite.com':
                $modx->switchContext('contextForMySite');
                $modx->setOption('setting1', 'Value1');
                $modx->setOption('setting2', 'Value2');
                break;


        2. If there are defaults for the option in system settings and context setting, which loads last (system, context, gateway)?
        I’ve tried setting the defaults null. If the context setting is null, the default system setting gets used (not the gateway setOption). If the system setting is null, then the value is null when the resource calls the option [[++setting1]].

        3. Does the option need to exist as a system or context setting prior to using setOption? I am assuming so or there would be nothing to set.

        Thanks,
        -Brian
          • 3677
          • 130 Posts
          Well, I gave up on setOption for this purpose and decided to try using Session variables. So far it seems to be working.

          Gateway Plugin:
          case 'www.mysite.com':
          	$_SESSION['var1'] ='value1';
          	$_SESSION['var2'] ='value2';
          	$_SESSION['var3'] = 'value3';
          	$modx->switchContext('mySiteContext');
          break;


          Then I created a snippet to retrieve a Session var:

          Snippet getSessionVar:
          <?php
          if (isset($_SESSION[$sessionVar])){
              $output = $_SESSION[$sessionVar];
          }else{
              $output = 'nothing_set';
          }
          return $output;


          And finally I just call the snippet in my resources, passing in the specific session var name I need:

          [[!getSessionVar? &sessionVar=`var1`]]


          Since I’m not using context settings any more, I’m not able to set "site_url" and "http_host" for each domain. But after my initial tests it looks like these are set automatically anyway. I’ll be testing it out more today. If anyone knows of a specific problem with this approach let me know.

          Thanks,
          -Brian
            • 22303 MODX Staff
            • 10,725 Posts
            When you switchContext, you are automatically applying the settings. If you setOption after the switchContext() call, then it should work for the remainder of that request.
              • 3677
              • 130 Posts
              Jason, I think you are saying that default context settings get applied when running switchContext. So using setOption after switchContext should override the default? That wasn’t working for me.

              After abandoning setting context settings via the gateway, I’ve got everything working with session vars except for site url is not resolving properly. I think it is being cached as the first domain to resolve in the gateway and all subsequent domains use that url which breaks links.

              I was using this throughout the site for links and to set a canonical URL:
              [[~[[*id]]? &scheme=`full`]]
              


              This is now resolving to the cached value for full URL scheme I think. I checked and "site_url" is actually rendering properly. This code that uses the current document ID with &scheme is pulling a cached value.

              -Brian
                • 18373 ☆ A M B ☆
                • 3,141 Posts
                Seeing how *everything* can be cached now, can you give this a try?

                [[!~[[*id]]? &scheme=`full`]]
                  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
                  Hey, good news Mark. This works!

                  Thanks,
                  -Brian