This is the first in a series of articles on dealing with issues created by operations performed in plugins that work on the Create/Edit Resource Panel in the MODX Manager.
In most plugins attached to Create/Edit Resource panel events, you just do your work and return. For some plugins, though, that's not enough. In the old days, MODX Revolution used to reload the Create/Edit Resource panel when you saved the Resource. That solved a lot of problems, but it slowed down an already slow Manager. Now that that's no longer the case, there are cases where you have to resort to some tricks to get things to work the way you want.
In this article, we'll look at the issues you might face with more complicated plugins. In the following articles, we'll examine various solutions to those issues.
Here are some of the topics we'll cover in this series of articles:
- Handling multiple MODX events in a single plugin
- Finding elements in a Manager page
- Making an AJAX processor call
- Duplicating resources in PHP
- Deleting resources in PHP
- Deleting resources vs. removing them
- Modifying the MOD Resources tree
- Popping up an "Are You Sure" dialog
- Reloading a MODX page when it's saved
heredocsyntax in PHP
- Using aliases to get MODX related objects
- Using the
modTemplateVarResourceobject to get and update TV values
onClick()to set button actions
- Forwarding users to another page with a button
A Use Case
I recently spent a lot of time working on some changes to the StageCoach extra that exposed a lot of interesting issues with plugins and the Create/Edit Resource Panel. StageCoach, in case you're not familiar with it, lets you record changes to a current resource and schedule them to be applied at some future date. If, for example, your client wants to advertise a special offer, but doesn't want the offer to appear until a particular date and time. StageCoach lets you create the changed page, then on the specified date, it replaces the page's content with the content from the staged page. I'll used StageCoach as an example in this and the following articles, so I need to explain a little about how it works, and give some information about the recent improvements to it.
StageCoach has always had two TVs — one for the Stage Date (and time) the changes would be implemented, and one for the ID of the staged resource (Staged Resource ID). To create a staged resource, you simply enter the date and save the resource. The StageCoach plugin was originally attached to the
OnDocFormSave, StageCoach looks at the two TVs. If the Stage Date TV is filled and the Staged Resource ID is empty, it simply duplicates the current page, adding the Stage Date to the pagetitle and putting the Staged Resource in the StageCoach Staged Resources folder.
OnWebPageInit, StageCoach checks the Staged Resource ID TV. If it has a value, StageCoach checks the Stage Date TV to see if it's time to replace the content. If so, it replaces the content of the current resource with the content of the Staged Resource, deletes the staged resource, and clears the two TVs on the Original Resource.
MODX user Michael Snow, of Snow Creative was kind enough to suggest some improvements to the StageCoach extra, and I want to thank him for testing the various iterations of the plugin. Michael pointed out some inconveniences and potentially serious problems for the clients who used StageCoach. For one thing, if users changed their mind about having a staged resource, they would often clear both the TVs and save the resource. This did prevent the changes from taking place, but it did not delete the staged resource, so staged resources tended to pile up, making it difficult to find one you wanted to work on and almost impossible to know which ones were real and which ones were left-over orphans.
Michael also pointed out that users often needed to jump back and forth between the staged resource and the original. For example, you might find a typo in the staged resource that needed to be fixed in the original. You might also want to revert a change in the staged resource and need to look at the original to find the old content. Micheal suggested that the ideal solution would involve buttons on the Create/Edit Resource panel: "Delete Draft," "Edit Draft," and when editing the staged resource, "Edit Original."
The issues and solutions involved are too many and too complicated to discuss in a single blog article, so we'll look at them in the following series of articles.
In the next few articles, we'll look at how to place extra buttons next to the "Save" and "View" buttons on the Create/Edit Resource panel.
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.)