Skip to content

Instantly share code, notes, and snippets.

@screeny05
Last active March 12, 2018 14:31
Show Gist options
  • Save screeny05/3e5e534aaaa5d9a9d9a45ba8be55cca8 to your computer and use it in GitHub Desktop.
Save screeny05/3e5e534aaaa5d9a9d9a45ba8be55cca8 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Sebastian Langer <[email protected]>
* @license MIT
*/
use Shopware\Bundle\StoreFrontBundle\Service\Core\ContextService;
use Shopware\Bundle\StoreFrontBundle\Gateway\DBAL\MediaGateway;
use Shopware\Components\Compatibility\LegacyStructConverter;
use Enlight_Template_Default;
use InvalidArgumentException;
/**
* Get the legacy media struct of the media-object by its id.
* Returns null if not found.
*
* @param array $params
* @param $template
* @throws InvalidArgumentException
*/
function smarty_function_legacyMediaById(array $params, Enlight_Template_Default $template)
{
/** @var ContextService $contextService */
$contextService = Shopware()->Container()->get('shopware_storefront.context_service');
/** @var MediaGateway $mediaGateway */
$mediaGateway = Shopware()->Container()->get('shopware_storefront.media_gateway');
/** @var LegacyStructConverter $legacyStructConverter */
$legacyStructConverter = Shopware()->Container()->get('legacy_struct_converter');
if (!array_key_exists('id', $params) || !array_key_exists('assign', $params)) {
throw new InvalidArgumentException('Arguments id and assign must be given.');
}
$id = $params['id'];
$assign = $params['assign'];
$context = $contextService->getContext();
$mediaStruct = $mediaGateway->get($id, $context);
$legacyMedia = $legacyStructConverter->convertMediaStruct($mediaStruct);
if (!is_array($legacyMedia)) {
$template->assign($assign, null);
} else {
$template->assign($assign, $legacyMedia);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment