<![CDATA[ What is the proper use of setOption to change a context setting? - My Forums]]> https://forums.modx.com/thread/?thread=82996 <![CDATA[What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-457982 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.]]>
bmilton Mar 07, 2013, 04:08 PM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-457982
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458148
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]]>
bertoost Mar 09, 2013, 03:27 PM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458148
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458145
The session stuff crops up when you use multiple contexts -- you'll have to check your session settings.]]>
Everettg_99 Mar 09, 2013, 12:48 PM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458145
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458124 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.]]>
bmilton Mar 09, 2013, 09:43 AM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458124
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458118
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
]]>
bmilton Mar 09, 2013, 08:56 AM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458118
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458084
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.

]]>
bmilton Mar 08, 2013, 08:00 PM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458084
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458076 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.]]>
Everettg_99 Mar 08, 2013, 05:18 PM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458076
<![CDATA[Re: What is the proper use of setOption to change a context setting?]]> https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458074
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.]]>
bertoost Mar 08, 2013, 04:52 PM https://forums.modx.com/thread/82996/what-is-the-proper-use-of-setoption-to-change-a-context-setting#dis-post-458074