Updating MODX Settings in Code

How to update various kinds of MODX settings in PHP


In the last few articles, we looked at how to create new MODX settings in code. In this one, we'll look at how to to update existing settings in PHP code.

MODX logo

Use Cases

The use cases for updating a setting in code are more common than you might think. You might, for example, have a plugin or snippet that needs to change the value of a setting based on certain conditions. In a previous article in this series, we used the example of a custom_theme setting that personalized the site for individual users. Ultimately, you'd want to present the user with a form that let them select a theme. The snippet that processes the form will have to update the setting.

Here's another example. Say you've developed a transport package that creates a new setting. During the installation process, you need to set the value of that setting, but you can't hard-code it into the package, because you can't know its value ahead of time. NewsPublisher, for example, has a System Setting called np_login_id. It needs to be set to the ID of the site's Login page. There's no way to know that ID ahead of time, so NewsPublisher (in a resolver) has to look for a page with the alias 'login' and use its ID (and issue a message if it's not found).

You might also have a setting that needs to be set to the ID of a resource, TV, or Template that your package creates. You can't set it ahead of time, because you won't know the ID of the object until after it's installed.

In the following sections, we'll use the custom_theme setting as an example. We'll change it's value to "midnight."


Updating a System Setting

The process of updating a setting is very straightforward. You get the setting object, set its value field, and save it. Here's the code for getting a System Setting:

$objectClass = 'modSystemSetting';
$key = 'custom_theme';
$newValue = 'midnight';

$c = array(
    'key' => $key,
);

$setting = $modx->getObject($objectClass, $c);

if ($setting) {
    $setting->set('value', $newValue);
    $setting->save();
} else {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Could not find ' . $objectClass . ' with key: ' . $key);
}

In the code above, $c contains the criteria for the search. In this case, we're saying that we want to search for a setting where the key field of the setting is 'custom_theme'. We'll see slightly more complex criteria in the sections below when we look at other kinds of settings .

Notice the if($setting) { line. This is important because if the setting is not found, calling its set() or save() member will result in a PHP fatal error. We need to be sure not to call them unless the setting is actually found.


Updating a Context Setting

The process of updating a Context setting is the same except that you change the $objectClass to modContextSetting, and there's an extra line in the criteria. Because there might be several Context Settings with the same key in different contexts, you need to specify the context of the context_key of the one you're looking for. Here's the code for updating a Context Setting:

$objectClass = 'modContextSetting';
$key = 'custom_theme';
$newValue = 'midnight';

$c = array(
    'key' => $key,
    'context_key' => 'web',
);

$setting = $modx->getObject($objectClass, $c);

if ($setting) {
    $setting->set('value', $newValue);
    $setting->save();
} else {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Could not find ' . $objectClass . ' with key: ' . $key);
}

Updating a User Group Setting

The process of updating a User Group setting is the same except that you change the $objectClass to modUserGroupSetting, and there's an extra line in the criteria. Because there might be several User Group Settings with the same key for different User Groups, you need to specify the ID of the of the group whose setting you're looking for. Here's the code for updating a User Group Setting:

$objectClass = 'modUserGroupSetting';
$key = 'custom_theme';
$newValue = 'midnight';

$c = array(
    'key' => $key,
    'group' => '12',
);

$setting = $modx->getObject($objectClass, $c);

if ($setting) {
    $setting->set('value', $newValue);
    $setting->save();
} else {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Could not find ' . $objectClass . ' with key: ' . $key);
}

Updating a User Setting

The process of updating a User setting is the same except that you change the $objectClass to modUserSetting, and there's an extra line in the criteria. Because there might be many Users with the same key for different Users, you need to specify the ID of the of the user whose setting you're looking for. Here's the code for updating a User Setting:

$objectClass = 'modUserSetting';
$key = 'custom_theme';
$newValue = 'midnight';

$c = array(
    'key' => $key,
    'user' => '12',
);

$setting = $modx->getObject($objectClass, $c);

if ($setting) {
    $setting->set('value', $newValue);
    $setting->save();
} else {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Could not find ' . $objectClass . ' with key: ' . $key);
}

Coming Up

In this article, we looked at how to update a single setting. You might wonder how to handle things when you want to update a number of different settings at the same time (for example, changing the value of a User Setting for all users). We'll look at how to do that in the next article.


Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)