Updating Manager Pages in a Plugin VIII

Reloading the Create/Edit Resource Panel when the Save button is clicked

This is the eighth in a series of articles dealing with issues created by operations performed in plugins that work on the Create/Edit Resource Panel in the MODX Manager. In the previous article, we added the code that creates a new Staged Resource, but there was an issue. If the user saved the page again after the Staged Resource was created, our Staged Resource ID TV got blanked out, orphaning the Staged Resource. The solution is to immediately reload the page, so the value of the Staged Resource ID will show up in the TV. As far as I know, there is no way to do this in PHP, so we'll use some very clever JavaScript developed by a couple of MODX Forum members.

MODX logo

Reloading a Manager Page on Save

You would think that you could just use $modx->sendRedirect() or $modx->sendForward() to forward the user to the current manager page. Unfortunately, because of the way plugins are processes, it won't work. There is a way, though, thanks to an incredibly clever JavaScript trick created by Gary Nutting, and improved by smg6511v2.

Here is the the JavaScript to force the reload, and the PHP code to inject it into the web page. It goes with our other code in the OnDocFormRender section. Important: it must be below that code that sets the two TV values and the current resource ID.

That code looks like this:

        <script type="text/javascript">
            Ext.override(MODx.panel.Resource, {
                originalSuccess: MODx.panel.Resource.prototype.success
                , success: function (o) {

                    var stageDateTv = document.getElementById("tv' . $stageDateTvId . '").value;
                    var stagedResourceTv = document.getElementById("tv' . $stagedResourceTvId . '").value;

                    if (!!stageDateTv && (!stagedResourceTv || !stagedResourceTv.length)) {
                        var url = location.href, i = url.indexOf("?") + 3;


What this JavaScript code does, essentially, is hijack the action of the "Save" button. It gets a reference to the original success function attached to the AJAX call for that "Save" button, then it calls that function (this.originalSuccess(o);) to make sure the original function executes. After doing that it checks to make sure the Stage Date TV is not empty and the Staged Resource ID TV is empty, that means that a Staged Resource will be created and the page needs to be reloaded. If those conditions are not met, it does nothing. If they are met, it calculates the last part of the URL for the current page, and calls MODx.loadPage() with that URL — page reloaded!.

This little bit of code could use some explanation:

if (!!stageDateTv && (!stagedResourceTv || !stagedResourceTv.length)) {

The first part, !!stageDateTv, is a JavaScript trick for turning the value of the Stage Date TV (which is a string), into a boolean (true/false) value that will be true if the TV has a value and false if it is empty. The second part will only be true if the Staged Resource TV is empty because it either doesn't evaluate to true, or doesn't have any length to it.


It would make sense to add this code to the part of our earlier code that gets injected only when the TV values warrant it. That way, for resources with no need for this code, it would never be injected in the first place. If we did that, there would be no need to get the TV values and test them. I left it in this form, however, for people who might just want working code to reload the page when it's saved, and an example of making that reload conditional based on TV values.

Coming Up

We're on the home stretch here, but we have one task left. It's nice to be able to create a Staged Resource, but we never actually do anything with it, and the Original Resource is never updated. We need to update the Original Resource and do some housekeeping when the Stage Date arrives for an Original with a Staged Resource. We'll see how to do that in the next article using the OnWebPageInit event.

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.