Skip to content

Commit

Permalink
Add method MediaInterface::getMediaOrder() and implement it
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 31, 2018
1 parent d7bd0bf commit 62a8d8b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
7 changes: 7 additions & 0 deletions system/src/Grav/Common/Media/Interfaces/MediaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public function getMedia();
* @return string|null Media path or null if the object doesn't have media folder.
*/
public function getMediaFolder();

/**
* Get display order for the associated media.
*
* @return array Empty array means default ordering.
*/
public function getMediaOrder();
}
14 changes: 13 additions & 1 deletion system/src/Grav/Common/Media/Traits/MediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ trait MediaTrait
*/
abstract public function getMediaFolder();

/**
* Get display order for the associated media.
*
* @return array Empty array means default ordering.
*/
abstract public function getMediaOrder();

/**
* Get URI ot the associated media. Method will return null if path isn't URI.
*
Expand Down Expand Up @@ -55,7 +62,7 @@ public function getMedia()
// Use cached media if possible.
$cacheKey = md5('media' . $this->getCacheKey());
if (!$media = $cache->fetch($cacheKey)) {
$media = new Media($this->getMediaFolder());
$media = new Media($this->getMediaFolder(), $this->getMediaOrder());
$cache->save($cacheKey, $media);
}
$this->media = $media;
Expand All @@ -72,6 +79,11 @@ public function getMedia()
*/
protected function setMedia(MediaCollectionInterface $media)
{
/** @var Cache $cache */
$cache = Grav::instance()['cache'];
$cacheKey = md5('media' . $this->getCacheKey());
$cache->save($cacheKey, $media);

$this->media = $media;

return $this;
Expand Down
6 changes: 4 additions & 2 deletions system/src/Grav/Common/Page/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ class Media extends AbstractMedia
protected $standard_exif = ['FileSize', 'MimeType', 'height', 'width'];

/**
* @param $path
* @param string $path
* @param array $media_order
*/
public function __construct($path)
public function __construct($path, array $media_order = null)
{
$this->path = $path;
$this->media_order = $media_order;

$this->__wakeup();
$this->init();
Expand Down
15 changes: 11 additions & 4 deletions system/src/Grav/Common/Page/Medium/AbstractMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class AbstractMedia extends Getters implements MediaCollectionInterface
protected $videos = [];
protected $audios = [];
protected $files = [];
protected $media_order;

/**
* Get medium by filename.
Expand Down Expand Up @@ -132,14 +133,20 @@ protected function add($name, $file)
*/
protected function orderMedia($media)
{
$page = Grav::instance()['pages']->get($this->path);
if (null === $this->media_order) {
$page = Grav::instance()['pages']->get($this->path);

if ($page && isset($page->header()->media_order)) {
$media_order = array_map('trim', explode(',', $page->header()->media_order));
$media = Utils::sortArrayByArray($media, $media_order);
if ($page && isset($page->header()->media_order)) {
$this->media_order = array_map('trim', explode(',', $page->header()->media_order));
}
}

if (!empty($this->media_order) && is_array($this->media_order)) {
$media = Utils::sortArrayByArray($media, $this->media_order);
} else {
ksort($media, SORT_NATURAL | SORT_FLAG_CASE);
}

return $media;
}

Expand Down
17 changes: 15 additions & 2 deletions system/src/Grav/Common/Page/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,14 +1149,27 @@ public function media($var = null)
return $this->getMedia();
}

/**
* Get filesystem path to the associated media.
*
* @return string|null
*/
public function getMediaFolder()
{
return $this->path();
}

/**
* Get display order for the associated media.
*
* @return array Empty array means default ordering.
*/
public function getMediaOrder()
{
$header = $this->header();



return isset($header->media_order) ? array_map('trim', explode(',', (string)$header->media_order)) : [];
}

/**
* Gets and sets the name field. If no name field is set, it will return 'default.md'.
Expand Down

0 comments on commit 62a8d8b

Please sign in to comment.