In the previous article, we looked at how to get the path and URL for a MODX Media Source, but the method we used always returns and absolute path and URL. In this one, we'll look at how to get the relative path and URL.
Relative Path and URL
This is slightly more complicated than the method we saw in the previous article. We need to call the
getBases() method of the Media Source and extract the relative path and URL from the array it returns. The code looks like this:
/** @var $ms 'modMediaSource' */ $mediaSourceId = 12; $output = ''; $ms = $modx->getObject('modMediaSource', $mediaSourceId); $ms->initialize(); $absolutePath = $bases['pathAbsolute']; $relativePath = $bases['path']; $absoluteUrl = $bases['urlAbsolute']; $relativeUrl = $bases['url']; $output .= "<br>Absolute Base Path: " . $absolutePath; $output .= "<br>Relative Base Path: " . $relativePath; $output .= "<br>Absolute URL: " . $absoluteUrl; $output .= "<br>Relative URL: " . $relativeUrl; return $output;
As we saw in the previous article, the URLs won't include the protocol or the server name, but you can create a full URL by prepending the MODX site URL:
$siteUrl = $modx->getOption('site_url'); $output .= "<br>Absolute URL: " . $siteUrl . $absoluteUrl;
In the next article, we'll look at a utility snippet that will empty the value of a specified TV for resources under one or more parent folders.
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.)