diff --git a/modules/cms/classes/MediaLibrary.php b/modules/cms/classes/MediaLibrary.php index 3c08ca42a..a7f87acba 100644 --- a/modules/cms/classes/MediaLibrary.php +++ b/modules/cms/classes/MediaLibrary.php @@ -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. diff --git a/modules/cms/formwidgets/MediaFinder.php b/modules/cms/formwidgets/MediaFinder.php index 56ea2f444..9a6e47b71 100644 --- a/modules/cms/formwidgets/MediaFinder.php +++ b/modules/cms/formwidgets/MediaFinder.php @@ -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', '', $this->prompt); $this->vars['mode'] = $this->mode; diff --git a/modules/cms/formwidgets/mediafinder/partials/_image_single.htm b/modules/cms/formwidgets/mediafinder/partials/_image_single.htm index a798beaa1..dd5e257f1 100644 --- a/modules/cms/formwidgets/mediafinder/partials/_image_single.htm +++ b/modules/cms/formwidgets/mediafinder/partials/_image_single.htm @@ -12,7 +12,7 @@
- +

diff --git a/modules/cms/twig/Extension.php b/modules/cms/twig/Extension.php index 1c20a66e3..7ff415832 100644 --- a/modules/cms/twig/Extension.php +++ b/modules/cms/twig/Extension.php @@ -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); } /**