On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I am attempting to use setOption to set a value on a context specific site setting.
    I've defined a context setting with name and key of "test".

    Then in my plugin which fires on every page load (onHandleRequest) I am trying to change the value of that context setting:

    $modx->switchContext('MySiteContext');
    $modx->setOption('test', 'Value1');


    When I reference this setting in my template it is empty. If I populate the context setting value directly through the manager, that value does show up.

    What am I missing? Seems that others have posted they use setOption successfully in a similar way.
    • Why do you want this? You can add (own, custom) settings at context-settings tab when editting a context. If you put it there, you can access it everywhere.

      Setting/saving a setting if you do would probably only work with

      $setting = $modx->newObject('modContextSetting');
      $setting->set('key', 'test');
      $setting->set('value', 'Value1');
      $setting->save();

      But again; add it normally through the manager by editting a context itself.
        MODX Ambassador (NL) & Professional MODX developer
        Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
        MODX e-commerce solution SimpleCart
      • I think you may be running into problems with the cache: when you use setOption, you set the value only for that one page request. The next time a page loads, the value comes from the database (see http://rtfm.modx.com/display/xPDO20/setOption).

        To create a setting programmatically, you'll want to do it like bertoost suggested (see http://rtfm.modx.com/display/revolution20/System+Settings#SystemSettings-CreatingaSystemSettingProgrammatically), but I think you'll need to involve the modContextSetting object either instead of modSystem or in addition to it.
        • The reason that I can't just set them on the context in the manager is that I am sending MANY domains to the exact same context via the gateway plugin. As part of the gateway routing I need to define site specific settings that will be used to populate the content used in that site context.

          So like Everett says I need to create a setting programmatically. I also want to be able to utilize page caching and have it work.

          • Hi guys,

            We were able to set these context options using this method suggested in another thread by Bob Ray:

            $modx->switchContext('MyContext');
            $modx->setOption('site_url', 'http://www.mySite.com/');$modx->setPlaceholder('+site_url', 'http://www.mySite.com/');


            That seems to work though I'd still like to try your method as well. Once the site_url is set on the context, I am referencing it to set the base url:

            <base href="[[!++site_url]]"></base>


            What I found is that this is not reliable when page caching is turned on for the context resource. With caching on the values will reflect those that were obviously set in the context of a different domain (not the current session).

            So it seems that the problem I have now is not so much how to set the option on the context but how to reference the option with page caching on and still get the correct option value I set in the gateway.

            I've found that I have this problem when referencing both site_settings I defined using the above method, standard resource settings like title, and tvs that are set on the resource. In all cases I am using the ! no cache option:

            <title>[[!*longtitle]]</title>
            <meta name="description" content="[[!*description]]" />
            <base href="[[!++site_url]]"></base>


            Any suggestions? I've turned off page caching on all resources that need to reference settings and it works, but that is almost all the site resources and I'm worried that performance will suffer.

            Thanks,
            -Brian
            • I've found that I have this problem when referencing both site_settings I defined using the above method, standard resource settings like title, and tvs that are set on the resource. In all cases I am using the ! no cache option:

              <title>[[!*longtitle]]</title>
              <meta name="description" content="[[!*description]]">
              


              So to clarify, the tvs are setup on the resource to use one of the context settings I defined.
              title: Help | [[!++site_name]].com


              And the gateway sets site_name like:
              $modx->setOption('site_name', 'MySite');$modx->setPlaceholder('+site_name', 'MySite');


              So it may not be that the tv is cached so much as the setting referenced in that tv is still cached. I have the same problem if I set a session var in the gateway and try to retrieve it in the resource. I'd like to figure out how I can reference both session vars and context settings without this caching issue.
              • Have you tried this on an uncached page?

                The session stuff crops up when you use multiple contexts -- you'll have to check your session settings.
                • I do not understand this at all.. Why do you set those keys by your gateway script?? You can add the settings to your context settings (try edit the context, go to tab "settings") and then use it like [[++site_name]] or any other key you enter in the context settings.

                  You can do this for every context, you can add keys whatever you like, even new/own keys will be possible to add and use the same way. You can use it all with the [[++ ]] syntax and in PHP you can retrieve it with $modx->getOption() method.

                  But why do it easy when it can be done hard ;-) hehe
                    MODX Ambassador (NL) & Professional MODX developer
                    Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
                    MODX e-commerce solution SimpleCart