Updating Manager Pages in a Plugin IX

Knowing whether it's time to update a resource with the staged version

This is the ninth 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 reloads a page when it's saved. In this article and the following ones, we'll put the final piece in place — the code that updates the Original Resource when the stage date arrives.

MODX logo

Is It Time to Act?

The first step in updating the Original Resource with the content from the Staged Resource is figuring out if it's time to do it. We'll place the code in the OnWebPageInit section of our plugin code, replacing the "For future Use" comment. Here's the code:

case 'OnWebPageInit':
    $stageDateTvId = $modx->getOption('stagecoach_stage_date_tv_id');
    if (empty($stageDateTvId)) {
            '[StageCoach] stagecoach_stage_date_tv_id  System Setting is empty');
        return '';
    $stagedResourceTvId = $modx->getOption('stagecoach_staged_resource_tv_id');
    if (empty($stagedResourceTvId)) {
            '[StageCoach] stagecoach_staged_resource_tv_id System Setting is empty');
        return '';

    $resourceId = $modx->resourceIdentifier;
    if (empty($resourceId)) {
            '[StageCoach] Resource ID is empty');
        return '';

    $tvr = $modx->getObject('modTemplateVarResource', array(
        'contentid' => $resourceId,
        'tmplvarid' => $stageDateTvId,
    if (!$tvr) {
        return '';

    /* Bail out if the date is empty */
    $date = $tvr->get('value');
    if (empty($date)) {
        return '';

    /* Bail out if the date is less than the current date/time */
    $timeStamp = strtotime($date);
    if (time() < $timeStamp) {
        return '';
    } else { /* It's time to update the Resource */

       /* The code for this section appears later in this series */


The first step is getting the IDs of our two TVs and the ID of the current page being loaded. If we can't get them, there's no point in proceeding because they haven't been set.

Once we have the TV IDs, and resource ID, we query the modTemplateVarResource object to see if there is a value set in the Stage Date TV for this resource. If there's no value set, we return without doing anything. The TVs may not even be attached to this resource.

The code to get the TV value might not work reliably if the TV might be set to a default value, since in that case there's no modTemplateVarResource record for it, but that doesn't apply to either of our TVs — one is always set using the Datepicker in the Manager, and the other is set by us automatically when a Staged Resource is created.

Next we get the value field of the modTemplateVarResource, which contains the actual stage date. If it's empty or less than the current date, we do nothing and return.

If we pass all those tests, the resource being loaded has a Staged Resource, and it's time to update the Original Resource with the content of the Staged Resource.

Coming Up

We've got the code that decided if it's time to update the Original Resource, but we still need the code that actually does it. We'll see how to do that in the next article. The code in that article will replace the comment at the end of the code above.

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.