Skip to content

Instantly share code, notes, and snippets.

@aragon999
Created November 15, 2019 15:14
Show Gist options
  • Save aragon999/a205ca650c5e732e3a34551c0e76b540 to your computer and use it in GitHub Desktop.
Save aragon999/a205ca650c5e732e3a34551c0e76b540 to your computer and use it in GitHub Desktop.
[Shopware] Check if a media exists in the template
<?php
function smarty_modifier_swkweMediaExists($mediaPath)
{
$mediaService = Shopware()->Container()->get('shopware_media.media_service');
if ($mediaService->has($mediaPath)) {
return $mediaPath;
} else {
return '';
}
}
@aragon999
Copy link
Author

Drop the file in /themes/Frontend/<MyTheme>/_private/smarty/, use it on a media path like the following:

{if $path|swkweMediaExists}
    {$path}
{/if}

or

{$path = $path|swkweMediaExists}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment