Creating Extras for Revo 3 Part 6: Media Sources in Code

Handling Media Source objects in the code of your Extra.

By Bob Ray
June 13, 2022
Creating Extras for Revo 3 Part 6: Media Sources in Code

In the previous article, we looked at how to handle Transport Package, and Transport Provider objects in the code of your Extra. In this one, we’ll see how to convert the code for Media Source objects.

This will mostly be a rehash of the previous article but with a new prefix. In the following article, when we get to processors, things will get a lot more interesting.

Media Sources as Objects

If your Extra never creates or modifies a modMediaSource object, you can skip this article.

In Revo 2, the code for Media Source objects looks like this:

$tPackage = $modx->getObject('sources.modMediaSource, $criteria);
/* Or */
$tPackage = $modx->newObject('sources.modMediaSource);

In Revo 3, it looks like this:

$tPackage = $modx->getObject('MODX\Revolution\Sources\modMediaSource', $criteria);
/* Or */
$tPackage = $modx->newObject('MODX\Revolution\Sources\modMediaSource');

Separate Package for Revo 3

If your Extra’s code is intended to run only in Revo 3, all you have to do for modMediaSource is to add the following lines near the top of your code (depending on your code, you probably only need the first one):

use MODX\Revolution\Sources\modMediaSource;
use MODX\Revolution\Sources\modMediaSourceElement;
use MODX\Revolution\Sources\modMediaSourceContext;

As an alternative, you can change the instances of any Media Source objects to include the namespace path. For example:

/* Change all instances of */
'sources.modMediaSource'
/* TO */
'MODX\Revolution\Sources\modMediaSource'

This works for modMediaSourceElement objects and modMediaSourceContext objects too, using the same prefix:

/* Change all instances of */
'sources.modMediaSourceElement'
/* TO */
'MODX\Revolution\Sources\modMediaSourceElement'

/* Change all instances of */
'sources.modMediaSourceContext'
/* TO */
'MODX\Revolution\Sources\modMediaSourceContext'

Usually, though, you want the Element for a specific Media Source when you already have the modMediaSource object. Or you have the modMediaSourceElement object, and you want its Context. In those cases, it’s simpler:

$element = $mediaSource->getOne('SourceElement');
$context = $mediaSourceElement->getOne('Context');

Notice that the aliases (“SourceElement”, and “Context”) are the same as in Revo 2. This is true of all the getOne() and getMany() aliases.

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 all the mediaSource-related objects.

$isMODX3 = $modx->getVersionData()['version'] >= 3;

$mediaSourcePrefix = $isMODX3
    ? 'MODX\Revolution\Sources\\'
    : 'sources.';

/* For media source objects */
$mediaSource = $modx->getObject($mediaSourcePrefix . 'modMediaSource', $criteria);
/* Or */
$mediaSource = $modx->newObject($mediaSourcePrefix . 'modMediaSource');

/* For media source elements */

$mediaSourceElement = $modx->getObject($mediaSourcePrefix . 'modMediaSourceElement', $criteria);
/* Or */
$mediaSourceElement = $modx->newObject($mediaSourcePrefix . 'modMediaSourceElement');

Note the capital S on “Sources” in the Revo 3 version. It’s important, as is the dot after “Sources” in the Revo 2 prefix.

The technique above will also work for modFileMediaSource and modS3MediaSource which extend modMediaSource, though usually a reference to the parent class (modMediaSource) will get you what you want, just as modResource will work for modDocument objects.


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.