Updating Manager Pages in a Plugin I

First in a series of articles on using a plugin to modify MODX Manager pages


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.

MODX logo

Preview

Here are some of the topics we'll cover in this series of articles:

  • Modifying a Manager page with JavaScript
  • Handling multiple MODX events in a single plugin
  • Injecting System Setting values into JavaScript code
  • Using MODX lexicon strings in JavaScript
  • Finding elements in a Manager page
  • Injecting JavaScript
  • Making an AJAX processor call
  • Duplicating resources in JavaScript
  • Duplicating resources in PHP
  • Deleting resources in JavaScript
  • 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
  • Using heredoc syntax in PHP
  • Using PHP code to generate dynamic JavaScript code that creates HTML code
  • Using aliases to get MODX related objects
  • Using the modTemplateVarResource object to get and update TV values
  • Calling JavaScript functions from a button on the screen
  • Using addEventListener() vs. 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 and OnManagerPageRender events.

In 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.

In 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."

Inserting the buttons into the Manager page was challenging, and it was lucky for me that Michael suggested some JavaScript code that helped get me started on that (more on this in the next article). Once the buttons were in place and working, a number of other problems showed up. For example, after the user clicked on the "Delete Draft" button, the draft was deleted and the values of the TVs were cleared in the DB, but the value still showed in the Stage Date TV on the screen. If the user reloaded the page immediately, everything was fine and the TVs were cleared. If the user made other changes to the page and saved it before reloading, though, the TVs got refilled in the DB and now pointed to a deleted resource. There were also some interesting problems in showing (and not showing) the buttons on the appropriate pages. The plugin had to be attached to a third event (OnDocFormRender).

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.


Coming Up

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.)



Comments (0)


Please login to comment.

  (Login)