diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ca0e77..4f492d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- fix - event when the boolean `must_store_when_generating_url` option is set to `true`, the media is not stored when generating a URL using the twig `joli_media_url` filter + ## [0.2.0] - 2024-12-01 - feature - added pagination to admin bridges media lists diff --git a/config/services.php b/config/services.php index 6752e40..37ae151 100644 --- a/config/services.php +++ b/config/services.php @@ -458,6 +458,7 @@ ->set('joli_media.twig_extension', JoliMediaExtension::class) ->args([ service('joli_media.resolver'), + service('joli_media.converter'), ]) ->tag('twig.extension') diff --git a/src/Twig/Components/Picture.php b/src/Twig/Components/Picture.php index be62a3e..fa0cc27 100644 --- a/src/Twig/Components/Picture.php +++ b/src/Twig/Components/Picture.php @@ -60,7 +60,7 @@ public function mount( } catch (MediaNotFoundException) { // the media cannot be found based on the path and library, so we create a fake media object // to allow the component to render - there will obviously be a 404 error when trying to access the media - $media = new Media($path, $this->libraries->getDefault()->getOriginalStorage()); + $media = new Media($path, $this->libraries->get($library)->getOriginalStorage()); } } diff --git a/src/Twig/JoliMediaExtension.php b/src/Twig/JoliMediaExtension.php index 8b181f7..ee14e9a 100644 --- a/src/Twig/JoliMediaExtension.php +++ b/src/Twig/JoliMediaExtension.php @@ -2,6 +2,7 @@ namespace JoliCode\MediaBundle\Twig; +use JoliCode\MediaBundle\Conversion\Converter; use JoliCode\MediaBundle\Model\Media; use JoliCode\MediaBundle\Model\MediaVariation; use JoliCode\MediaBundle\Resolver\Resolver; @@ -13,6 +14,7 @@ class JoliMediaExtension extends AbstractExtension { public function __construct( private readonly Resolver $resolver, + private readonly Converter $converter, ) { } @@ -40,7 +42,17 @@ private function getUrl( ?string $libraryName = null, int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH, ): ?string { - return $this->getMediaVariation($path, $variationName, $libraryName)?->getUrl($referenceType); + $media = $this->getMediaVariation($path, $variationName, $libraryName); + + if ($media instanceof MediaVariation) { + try { + $this->converter->convertIfMustStoreWhenGeneratingUrl($media); + } catch (\Exception) { + // ignore errors + } + } + + return $media?->getUrl($referenceType); } private function getMediaVariation(