A MODX Forum user (scoder) asked for an easier way to toggle the site_status System Setting. If this is something you do often, it's a pain to have to go to the System Settings grid, find the setting, and change its value. In This article we'll look at a fairly simple widget that will let you toggle the site_status setting with a single click. The article is based on an idea from Susan Sottwell.
Although we're using the site_status setting as an example, this widget could be used to toggle the value of any Yes/No System Setting
The Concept
The idea is to embed a simple HTML form in the widget. When the "Submit" button is clicked, a bit of PHP code toggles the System Setting. When the setting is changed, so is the text of the "Submit" button.
Creating the Widget
Here are the steps for creating the Widget:
- Go to System (gear icon) -> Dashboards
- Click on the Widgets tab
- Click on the "Create New Widget" tab
- Fill in the Name: Toggle Site Status
- Set the size to "Half"
- Set the Widget Type to Inline PHP Widget
- Paste this code into the Widget Content Field:
<?php $output = ''; if (isset($_POST['submitVarToggle'])) { /* Submit button clicked */ /* Set the System Setting Object */ $setting = $modx->getObject('modSystemSetting', array('key' => 'site_status')); $status = $setting->get('value'); $status = $status ? '0' : '1'; $setting->set('value', $status); $setting->save(); /* Clear the System Settings cache */ $cm = $modx->getCacheManager(); $cm->refresh(array('system_settings' => array())); } else { $status = $modx->getOption('site_status'); } /* Set a variable for the text of Submit button based on the current status */ $statusMessage = $status? ' Take Site Offline ' : ' Bring Site Online '; /* Display the form */ $output .= ' <form action = "" method="post"> <input style="padding:10px" type="submit" name="submitVarToggle" value="' . $statusMessage . '" /> </form>'; return $output; - Click on the "Save" button
- Click on the "Close" button
The Code
The code above is fairly self-explanatory. The only thing that might be confusing are the two lines that actually do the toggling. The first one toggles the System Setting, the second one toggles the text of the Submit button:
$newStatus = empty($status) ? '1' : '0'; $statusMessage = $status? ' Take Site Offline ' : ' Bring Site Online ';
Both line use the PHP ternary operator. Basically, they tell PHP that if the part before the question mark is true, use the value just after the question mark. If not, use the value of the part after the colon. This is just a shorthand way of expressing an if else statement. The following code would be the equivalent of the first line:
if ($status) {
$status = '0';
} else {
$status = '1';
}
The second line is equivalent to this code:
if ($status) {
$statusMessage = ' Take Site Offline ';
} else {
$statusMessage = ' Bring Site Online ';
}
Connecting the Widget to the Dashboard
Now that we've created the Widget, we need to attach it to a dashboard so it will show up in the Manager. Here are the steps for that:
- Click on the "Dashboards" tab
- Right-click on the Dashboard you want to add the widget to and select "Update Dashboard"
- Click on "Place Widget"
- Use the drop-down menu to select your widget
- Click on the "Save" button in the dialog
- Click on the "Save" button at the upper right
Adapting the Widget to other System Settings
This widget can be used to toggle any Yes/No System Setting. The only things we'd need to change are the name of the setting and the two text messages for the Submit button.
The widget could also be adapted to other types of settings. To do that, you'd just need to change the object retrieved from modSystemSetting to modContextSetting, modUserSetting, or modUserGroupSetting.
Enhancements
The only drawback of our widget is that when the button is clicked, it forces a reload of the Manager page. This could be avoided, but it would make things a bit more complicated. We'd have to create a namespace for our widget, a controller to display the form, and a processor to toggle the setting. We'd call the processor with an Ajax call triggered by the click on the Submit button. If your site has a lot of resources, reloading the Manager page takes a long time, and the Settings will be changed often, this might be worth the effort. For most cases, though, the simple form above should be adequate.
If you have a lot of System Settings to control, you should consider installing Mark Hamstra's excellent ClientConfig extra. It takes a little effort to set up, but it provides a very nice interface for modifying System Settings.
Coming Up
In the next article, I'll discuss a particularly baffling 403 Permission Denied error for a single page (hint: It wasn't mod_security).
For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.
Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at Hosting.com (formerly A2 Hosting). (More information in the box below.)

Comments (1)
michelle84 — Jul 24, 2016 at 05:39 PM
Great post! Nice and simple way to toggle system settings.
Please login to comment.