MODX Community

Get basepath from Media source by id?

IS there a way to get the basepath form a media source in a snippet?
I’m using Image+ and need to get the image URL including the path of the media source it uses.
I only know the media source ID so what is the quickest way to get the PATH form a media source?
TNX
RDG

the snippet addmediasourcepath from here could do that
https://docs.modx.com/extras/revo/migxdb/migxdb.tutorials/migxdb.create-a-basic-gallery-management-from-scratch-with-migxdb/add-resource-specific-mediasource-and-multifile-uploader-to-the-gallery

This might work (assuming that it’s a file media source object):

$ms = $modx->getObject('modFileMediaSource', $mediaSourceId);
$bases = $ms->getBases();

$absolutePath = $bases['pathAbsolute'];
$relativePath  = $bases['pathRelative'];

It might be more correct to call getObject() with the parent class if it works:

$ms = $modx->getObject('modMediaSource', $mediaSourceId);

Tnx Bobray,

that works

Here’s a half finished snippet to get media source attributes that might be useful.

Needs work but might provide a fast start for someone.

<?php
/**
 * getMediaSourceAttribute
 *
 * DESCRIPTION
 *
 * Gets specified attribute of specified media source id
 *
 * PROPERTIES:
 *
 * &msId: string, optional, id of media source to get attribute from, defaults to default media source
 * &attribute: string, required, attribute to get value for from media source.
 *          options: basePath, baseUrl, thumbnailQuality, skipFiles
 * 
 * DEPENDENCIES:
 * 
 * None
 *
 * USAGE:
 *
 * [[getMediaSourceAttribute?
 *   &attribute=`basePath`
 *   &msId=`2`
 * ]]
 *
 */

    // check we have $modx
    if (!isset($modx)) {
        $modx->log(modX::LOG_LEVEL_ERROR, '[getMediaSourceAttribute] $modx not set.');
        return;
    }
    // check we have $attribute
    if (!isset($attribute)) {
        $modx->log(modX::LOG_LEVEL_ERROR, '[getMediaSourceAttribute] $attribute not set.');
        return;
    }
    
    /***** only check msId is integer if was specified
    // if id not integer log error and exit
    if ( !filter_var($msId, FILTER_VALIDATE_INT) ) {
        $modx->log(modX::LOG_LEVEL_ERROR, '[getMediaSourceAttribute] &msId is not an integer.');
        return;
    }
    */
    
    /**
     * Get media source properties for default media source
     */
    
    // get id of default_media_source
    $default_media_source   = $modx->getOption('default_media_source');
    
    // get modMediaSource object using default_media_source id
    $obj_media_source       = $modx->getObject( 'modMediaSource', array( 'id' => $default_media_source ) );
    
    // get modMediaSource properties
    $ms_props               = $obj_media_source->get( 'properties' );
    $ms_basePath            = $ms_props[ 'basePath' ][ 'value' ];
    
    
    
    // figure what to return
    switch ($attribute){
    	case "basePath":
    		return $ms_basePath;
    		break;
    	default:
    	    $modx->log(modX::LOG_LEVEL_ERROR, '[getMediaSourceAttribute] switch could not find requested attribute: ' . $attribute);
    	    return;
    }