Updating Manager Pages in a Plugin XI

The final article in this series shows how to transfer TV values from one resource to another


This is the eleventh, and final, 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 updates the Original Resource. In this article, we'll put the final piece in place — the code that (optionally) transfers the TV values from the Staged Resource to the Original Resource.

MODX logo

Updating the TVs

I've put this code in a separate article because it shows a simple, but extremely efficient, method for transferring TV values from one resource to another.

Here's the Code:

/* See if we need to transfer TV values from the staged resource */
$includeTvs = $modx->getOption('stagecoach_include_tvs', null, false);

/* Transfer TV values if option is set */
if ($includeTvs) {
    $tvrs = $stagedResource->getMany('TemplateVarResources');

    foreach ($tvrs as $oldTemplateVarResource) {
        /** @var $tvr modTemplateVarResource */
        /** @var $oldTemplateVarResource modTemplateVarResource */
        /** @var $newTemplateVarResource modTemplateVarResource */
        $value = $oldTemplateVarResource->get('value');

        $c = array(
            'contentid' => $resourceId,
            'tmplvarid' => $oldTemplateVarResource->get('tmplvarid'),
        );
        /* get Tvr for current resource and set it from staged resource */
        $tvr = $modx->getObject('modTemplateVarResource', $c);
        if ($tvr) {
            $tvr->set('value', $value);
            $tvr->save();
        } else { /* tvr does not exist -- create it */
            $tvr = $modx->newObject('modTemplateVarResource');
            $tvr->set('contentid', $resourceId);
            $tvr->set('tmplvarid',
                $oldTemplateVarResource->get('tmplvarid'));
            $tvr->set('value', $value);
            $tvr->save();
        }
    }
}

The code above replaces the "/* Transfer TVs here if option is set (next article) */" comment in the previous article. It should be indented to the appropriate depth.

First, we see if the option to include TVs is set by checking the stagecoach_include_tvs System Setting. If it is, we get the collection of modTemplateVarResource objects associated with the Staged Resource with this code:

$tvrs = $stagedResource->getMany('TemplateVarResources');

We could have retrieved the TVRs (perhaps a tiny bit faster) with this code:

getCollection('modTemplateVarResource', array('contentid' => $stagedResource->get('id'));

Using getMany(), though. is shorter and clearer, and it demonstrates a very convenient way to get MODX related objects using an alias (in this case, 'TemplateVarResources'. Notice that unlike other MODX objects, aliases always start with a capital letter, and if they get a collection of objects (as getMany{) does) rather than a single object, they are always plural.

Next, we loop through all the TVRs, getting the value of each one. Then we check to see if there is already a TVR for that TV and the current resource. If so, we set its value, if not, we create it, then set its value.


A Word of About TV Raw Values

You might think that this code could cause trouble because it assumes that it will always work to transfer just the raw values of the Staged Resource TV to the Original Resource. If any of the TVs contain properties (like the alt property for an image TV, or the delimiter value from a list TV), those properties won't be transferred because they reside in the TV object itself (modTemplateVar), not in the modTemplateVarResource object. This isn't a problem, though, because those properties are set in the TV itself, so they should act as they always did, and if they're changed, the change would be available for any future use of the TV anywhere on the site. Even an @ binding TV should work fine since the @ binding would be part of its raw value. The same is true for TVs with MODX tags in them.

You only need to worry about the processed (versus raw) values of a TV when you're rendering the TVs on the screen.


Wrapping Up

We've reached the end of this long series of article. If you've made it this far, I commend you. If not, you're not reading this. ;)

We've seen a lot of interesting techniques in this series. Here are some of them:

  • 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

I'm sure there are others that we've both forgotten. I had a good time writing this series and I hope you've enjoyed reading it.



Coming Up

In the next article, we'll look at a method for getting a TV value for a TV connected to a resource in another context..


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)