In the previous article, we looked at how to deal with most MODX objects in the code of your Extra to let it run well in Revo 3. In this one, we’ll at look how to handle Transport Package, and Transport Provider objects in the code of your Extra.
Transport Packages and Transport Providers as Objects
The odds are that you’ll never need to use this information, but I had to figure it out in order for the SiteCheck Extra to run in both Revo 2 and Revo 3, so I thought I’d share it with you just in case.
In Revo 2, the code for Transport Package objects looks like this:
$tPackage = $modx->getObject('transport.modTransportPackage, $criteria); /* Or */ $tPackage = $modx->newObject('transport.modTransportPackage);
In Revo 3, it looks like this:
$tPackage = $modx->getObject('MODX\Revolution\Transport\modTransportPackage', $criteria); /* Or */ $tPackage = $modx->newObject('MODX\Revolution\Transport\modTransportPackage');
Separate Package for Revo 3
If your Extra’s code is intended to run only in Revo 3, all you have to do for Transport Package objects is make the following changes:
For any PHP file that refers to
modTransportPackage objects or
modTransportProvider objects, insert the following near the top of the file:
use MODX\Revolution\Transport\modTransportPackage; use MODX\Revolution\Transport\modTransportProvider;
Depending on what you’re doing with the objects, you may also need one or both of these lines there:
use xPDO\Transport\xPDOTransport; use MODX\Revolution\Processors\Model\GetProcessor;
Instead of the
use statements above, you can also do this:
/* Change all instances of */ 'transport.modTransportPackage' /* TO */ 'MODX\Revolution\Transport\modTransportPackage'
This works for Transport Providers too, using the same prefix:
/* Change all instances of */ 'transport.modTransportProvider' /* TO */ 'MODX\Revolution\Transport\modTransportProvider'
Usually, though, you want the provider for a specific package when you already have the Transport Package object. In that case, it’s simpler:
$tProvider = $package->getOne('Provider');
Notice that the alias (“Provider”) is the same as in Revo 2. This is true of all the
Single Package for Both Revo 2 and Revo 3
In code intended to run in both Revo 2 and Revo 3, the solution is similar to the examples in the previous article, but with a different prefix, which can be used for both Transport Package objects and Transport Provider objects.
$isMODX3 = $modx->getVersionData()['version'] >= 3; $transportPrefix = $isMODX3 ? 'MODX\Revolution\Transport\\' : 'transport.'; /* For transport package objects */ $tPackage = $modx->getObject($transportPrefix . 'modTransportPackage', $criteria); /* Or */ $tPackage = $modx->newObject($transportPrefix . 'modTransportPackage'); /* For transport providers */ $tProvider = $modx->getObject($transportPrefix . 'modTransportProvider', $criteria); /* Or */ $tProvider = $modx->newObject($transportPrefix . 'modTransportProvider');
Note the capital T on “Transport” in the Revo 3 version. It’s important. Also, remember the double backslash at the end of the prefix. It’s necessary to make it clear to PHP that we’re not escaping the final single quote, we’re inserting a literal backslash.
Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.