Reading a setting is easy as MODX merges system, context (and user settings) at runtime.
So using this line of code you will get a value first from user settings overriding context settings overriding system settings (in this order). You don't have to know which one exists.
$setting = $modx->getOption('my_setting');
My question is: Is there also an easy method to change a settings value in reverse order without knowing if it exists in context or system?
Thanks,
Martin
There's some info here that might help:
http://bobsguides.com/blog.html/2013/06/19/displaying-modx-context-settings/.
It depends on what you mean by easy.
This should get you started:
<?php
$key = 'my_setting';
if (! isset($scriptProperties[$key])) {
/* none of them exist */
}
$csObj = $modx->getObject('modContextSetting',
array(
'context_key' => 'web', // or whatever
'key' => $key,
)
};
$ssObj = $modx->getObject('modSystemSetting',
array(
'key' => $key,
)
};
/* Not sure about this one */
$props = array();
$snippet = 'mySnippet';
$snippetObj = $modx->getObject('modSnippet', array('name' => $snippet));
if ($snippetObj) {
$props = $snippet->getProperties();
}
if (! empty($props) {
if (in_array($key, $props) {
$value = $props[$key]
} else {
/* no property */
}
}
if ($csObject) {
$value = $csObj->get('value');
} else {
/* no context setting */
}
if ($ssObject) {
$value = $ssObj->get('value');
} else {
/* no System Setting */
}
Remember that any of these can exist but be empty.
Hi Bob,
as always very helpful information! This is what I needed. I'll adapt it to work in my project!
Thanks,
Martin