Added Medium::getAlternatives() to be able to list all the retina sizes

This commit is contained in:
Matias Griese 2022-01-12 12:00:41 +02:00
parent 9b2af98b0b
commit bda6b26896
3 changed files with 28 additions and 1 deletions

View File

@ -7,6 +7,7 @@
* Added support for `assets.addJsModule()` with full pipeline support
* Added `Utils::getExtensionsByMime()` method to get all the registered extensions for the specific mime type
* Added `Media::getRoute()` and `Media::getRawRoute()` methods to get page route if available
* Added `Medium::getAlternatives()` to be able to list all the retina sizes
2. [](#improved)
* Improved `Utils::download()` method to allow overrides on download name, mime and expires header
* Improved `onPageFallBackUrl` event

View File

@ -63,6 +63,14 @@ interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObj
*/
public function addAlternative($ratio, MediaObjectInterface $alternative);
/**
* Get list of image alternatives. Includes the current media image as well.
*
* @param bool $withDerived If true, include generated images as well. If false, only return existing files.
* @return array
*/
public function getAlternatives(bool $withDerived = true): array;
/**
* Return string representation of the object (html).
*

View File

@ -128,11 +128,29 @@ trait MediaObjectTrait
}
$alternative->set('ratio', $ratio);
$width = $alternative->get('width');
$width = $alternative->get('width', 0);
$this->alternatives[$width] = $alternative;
}
/**
* @param bool $withDerived
* @return array
*/
public function getAlternatives(bool $withDerived = true): array
{
$alternatives = [];
foreach ($this->alternatives + [$this->get('width', 0) => $this] as $size => $alternative) {
if ($withDerived || $alternative->filename === basename($alternative->filepath)) {
$alternatives[$size] = $alternative;
}
}
ksort($alternatives);
return $alternatives;
}
/**
* Return string representation of the object (html).
*