Updating Multiple MODX Settings in Code

Using a snippet to automate the process of updating multiple settings


In the last article, we looked at how to update single MODX settings in code. In this one, we'll look at how to to update multiple settings in PHP code.

MODX logo

A Use Case

There may be occasions where you want to update a whole bunch of settings with the same key. We've used the example of a custom_theme setting before. Imagine that a bunch of users now have User Settings for that key with a value of midnight. You've discovered that the midnight theme has some serious problems and you've decided to replace it with a theme called darknight.

It would be a real pain to edit every user and change the theme for those already set to midnight. It would also be a pain to run the code from the previous article over an over, once for each user (and you'd have to figure out which users have midnight as their current theme).

We'll assume that you've already installed the darknight theme as described in an earlier article and all you need to do it change the User Settings that contain midnight in their value field.


The Code

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

$c = array(
    'key' => $key,
    'value' => $oldValue,
    // 'user' => '12', we no longer need this
);

$settings = $modx->getCollection($objectClass, $c);

foreach ($setting as $setting) {
        $setting->set('value', $newValue);
        $setting->save();
}

Because we want multiple selections, we've changed the call from getObject() to getCollection(). We've also changed the criteria. We no longer want a particular user, instead, we want all the user records where the value field contains darknight.

We no longer need the if ($setting) { line to see if we have a setting because anything getCollection() retrieves is guaranteed to be a setting. If no records are found, getResources() will return an empty array and the code inside the foreach loop will never execute.


Multiple Setting Types

What if you want to modify more than one kind of setting? You might have a custom_theme System Setting, Context Setting, User Setting, and UserGroup setting. You could modify the code above and run it four times, but it's not that difficult to do it all at once by creating an outer loop that loops through the types of settings:

$key = 'custom_theme';
$oldValue = 'midnight';
$newValue = 'darknight';

$objectClasses = array(
    'modSystemSetting',
    'modContextSetting',
    'modUserGroupSetting',
    'modUserSetting';
};
$c = array(
    'key' => $key,
    'value' => $oldValue,
);

foreach ($objectClasses as $objectClass) {
    $settings = $modx->getCollection($objectClass, $c);
    foreach ($setting as $setting) {
            $setting->set('value', $newValue);
            $setting->save();
    }
}

In the code above, we've changed the objectClass variable to the objectClasses array. The outer foreach loop loops through the setting types. The inner loop loops through all the settings for a certain setting type that meet the criteria. If any of the setting types don't contain a setting matching the criteria, getCollection() will return an empty array and the inner loop will be skipped.


Coming Up

In the next article, we'll revisit an old topic: how to change the name of the default user. Now there's a much easier way to do it.


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)