Skip to content

Commit

Permalink
Added MediaTrait::getMediaUri()
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 9, 2018
1 parent d434d51 commit 342aa0f
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions system/src/Grav/Common/Media/Traits/MediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Grav\Common\Grav;
use Grav\Common\Media\Interfaces\MediaCollectionInterface;
use Grav\Common\Page\Media;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;

trait MediaTrait
{
Expand All @@ -17,6 +18,29 @@ trait MediaTrait
*/
abstract public function getMediaFolder();

/**
* Get URI ot the associated media. Method will return null if path isn't URI.
*
* @return null|string
*/
public function getMediaUri()
{
$folder = $this->getMediaFolder();

if (strpos($folder, '://')) {
return $folder;
}

/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
$user = $locator->findResource('user://');
if (strpos($folder, $user) === 0) {
return 'user://' . substr($folder, strlen($user)+1);
}

return null;
}

/**
* Gets the associated media collection.
*
Expand Down

0 comments on commit 342aa0f

Please sign in to comment.