Add |media filter and MediaLibrary::url helper

This commit is contained in:
Samuel Georges 2015-06-17 18:51:05 +10:00
parent 8c6a58981e
commit 91c061f60a
4 changed files with 37 additions and 11 deletions

View File

@ -92,11 +92,13 @@ class MediaLibrary
$cached = Cache::get('cms-media-library-contents', false);
$cached = $cached ? @unserialize($cached) : [];
if (!is_array($cached))
if (!is_array($cached)) {
$cached = [];
}
if (array_key_exists($fullFolderPath, $cached))
if (array_key_exists($fullFolderPath, $cached)) {
$folderContents = $cached[$fullFolderPath];
}
else {
$folderContents = $this->scanFolderContents($fullFolderPath);
@ -391,6 +393,16 @@ class MediaLibrary
return $path;
}
/**
* Helper that makes a URL for a media file.
* @param string $file
* @return string
*/
public static function url($file)
{
return static::instance()->getPathUrl($file);
}
/**
* Returns a public file URL.
* @param string $path Specifies the file path relative the the Library root.

View File

@ -2,6 +2,7 @@
use Lang;
use ApplicationException;
use Cms\Classes\MediaLibrary;
use Backend\Classes\FormWidgetBase;
/**
@ -66,7 +67,9 @@ class MediaFinder extends FormWidgetBase
*/
public function prepareVars()
{
$this->vars['value'] = $this->getLoadValue();
$value = $this->getLoadValue();
$this->vars['value'] = $value;
$this->vars['imageUrl'] = $value ? MediaLibrary::url($value) : '';
$this->vars['field'] = $this->formField;
$this->vars['prompt'] = str_replace('%s', '<i class="icon-folder"></i>', $this->prompt);
$this->vars['mode'] = $this->mode;

View File

@ -12,7 +12,7 @@
<!-- Existing value -->
<div class="find-object">
<div class="icon-container">
<img data-find-image src="" alt="" />
<img data-find-image src="<?= $imageUrl ?>" alt="" />
</div>
<div class="info">
<h4 class="filename">

View File

@ -70,6 +70,7 @@ class Extension extends Twig_Extension
return [
new Twig_SimpleFilter('page', [$this, 'pageFilter'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('theme', [$this, 'themeFilter'], ['is_safe' => ['html']]),
new Twig_SimpleFilter('media', [$this, 'mediaFilter'], ['is_safe' => ['html']]),
];
}
@ -162,6 +163,19 @@ class Extension extends Twig_Extension
return $result;
}
/**
* Looks up the URL for a supplied page and returns it relative to the website root.
* @param mixed $name Specifies the Cms Page file name.
* @param array $parameters Route parameters to consider in the URL.
* @param bool $routePersistence By default the existing routing parameters will be included
* when creating the URL, set to false to disable this feature.
* @return string
*/
public function pageFilter($name, $parameters = [], $routePersistence = true)
{
return $this->controller->pageUrl($name, $parameters, $routePersistence);
}
/**
* Converts supplied URL to a theme URL relative to the website root. If the URL provided is an
* array then the files will be combined.
@ -174,16 +188,13 @@ class Extension extends Twig_Extension
}
/**
* Looks up the URL for a supplied page and returns it relative to the website root.
* @param mixed $name Specifies the Cms Page file name.
* @param array $parameters Route parameters to consider in the URL.
* @param bool $routePersistence By default the existing routing parameters will be included
* when creating the URL, set to false to disable this feature.
* Converts supplied file to a URL relative to the media library.
* @param string $file Specifies the media-relative file
* @return string
*/
public function pageFilter($name, $parameters = [], $routePersistence = true)
public function mediaFilter($file)
{
return $this->controller->pageUrl($name, $parameters, $routePersistence);
return $this->controller->mediaUrl($file);
}
/**