How to apply some system settings to multiple contexts at once?

I have big website which has 5 languages, + 3 sections and each section has 2-3 languages. For each variation I created a context and currently I have 11 contexts. I need different system settings for each section. What is the best way to create a system settings which is applied to some contexts ?

For example I contexts :
web,web-de,web-en,web-ru,web-it
ivf,ivf-hr,ivf-ru
onk, onk-en, onk-de

I want to apply some system settings to all contexts beginning with web, some other settings to context begining with ivf and different with those which start with onk.

I created a snippet which I call at teplate start where I define speciffic placeholders instead of system settings. Works fine but I’m wondering if there is a better way?

It sounds like you want Context Settings, which will override the System Settings with the same key. Use System Settings for values that will apply to all contexts and Context Settings for the ones specific to a given context.

If you’re doing it in a snippet, it would look something like this for a given context + key (untested):

$setting = $modx->getObject('modContextSetting', array('context_key' => 'some_context', 'key' => 'some_key'));
$setting->set('value', 'some_value');
$setting->save();

The Context Settings will have to exist or this will crash. You can modify it to create the Context Settings if they don’t exist, but it will run faster if you create them all first by hand.

If the values will be needed immediately, you’ll also probably need to do this at the end:

$modx->reloadContext($context);

Where $context is the name of the context;

If there are a lot of contexts and you want to use your prefix as a key, you can create a “like” query to get the modContextSetting objects.

I use system settings for general values but when I need something speciffic for all context which starts with web I need to insert the same key and value 5 times. At start is not a problem but when you have to change something you always must go though all speciffic contexts. This is the reason why I use a snippet. I check the context key value of a resource and set placeholder with needed value.

Your example sets the value and save it directly to context which is ok, but this is needed only on changes and not on every request. That poor snippet will become tired of saving settings so many times :slight_smile:

Would be nice to have “context group” and put settings there.

How about just using the same snippet tag everywhere and have the snippet return the correct value based on a ‘key’ property and a strpos() search of the the context_key of the current resource (maybe this is what you’re already doing)?

If you don’t want to edit the snippet code every time something changes, you could have the snippet parse a chunk containing the contexts, keys, and values.

Hm … just using a snippet instead of seting placeholders. Why I didn’t thought about such simple solution ??

Would be nice if I could use property sets on snippets for such cases.

thanx for advice.

There’s nothing I can think of that would prevent you using property sets, as long as they’re all connected to the snippet in System -> Property Sets.

[[!snippetName@propertySetName]]