Displaying MODX Context Settings

Use a simple snippet to show the value of Context Settings from a Context other than the current one

A user on the MODX Forums asked for a way to show a Context Setting from a Context other than the current one. In this case the setting was the site_start setting (I think the user wanted to use it in a link), but the code below will show any Context setting.

As you probably know, you can show a Context Setting for the current Context with a simple setting tag:


The tag above will be replaced by the site_start Context Setting if one is set. If not, it will be replaced by the System Setting (unless there is a site_start User Setting and the user is logged in). User Settings have the top priority, then Context Settings, then System Settings.

A setting tag will always show the Context Setting or System Setting for the current Context unless it's overridden by a User Setting. If you want to refer to another context, it gets a little trickier. Here's a snippet that will get a setting from the named context, and will report any errors in the process.

Here is the snippet tag:

[[getContextSetting? &context=`someContext` &setting=`site_start`]]

Here is the code to paste into the getContextSetting snippet:

/* getContextSetting snippet */

$ctx = $modx->getOption('context', $scriptProperties, null);
$setting = $modx->getOption('setting', $scriptProperties, null);

if ($ctx == null) {
    return 'No Context set';
} elseif ($setting === null) {
    return 'No Setting set';
} else {
    $csObj = $modx->getObject('modContextSetting',
            'context_key' => $ctx,
            'key' => $setting

if ($csObj) {
    return $csObj->get('value'); 
} else {
    return 'Context Setting not found';

If you've put the correct Context and Setting-name values in the tag, the tag will be replaced by the value of the named key from the specified Context.

Note that unlike using a setting tag, this code gets the Context Setting directly, so it will never show a System Setting or User Setting. That means it can also be used to get a Context Setting in the current context and the value won't be overridden by a User Setting of the same name.

Comments (0)

Please login to comment.