<?php declare(strict_types=1);
namespace DonCarneTheme\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
class FooterPageletLoadedSubscriber implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
private EntityRepositoryInterface $mediaRepository;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepositoryInterface $mediaRepository
){
$this->systemConfigService = $systemConfigService;
$this->mediaRepository = $mediaRepository;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'addConfigData'
];
}
/**
* Loads social media and quality certificate images. links can be taken directly in template
*/
public function addConfigData(FooterPageletLoadedEvent $event)
{
$themeConfig = $this->systemConfigService->get("DonCarneTheme.config");
if(!$themeConfig){
return;
}
$qualityIconArr = array_filter($themeConfig, function($key){
return str_starts_with($key, "qualityCertImage");
}, ARRAY_FILTER_USE_KEY);
$socialMediaIconArr = array_filter($themeConfig, function($key){
return str_starts_with($key, "socialMediaImage");
}, ARRAY_FILTER_USE_KEY);
// merge arrays
$mediaIds = array_merge($qualityIconArr, $socialMediaIconArr);
// remove empty values
$mediaIds = array_filter($mediaIds);
//load images
$criteria = new Criteria($mediaIds);
$mediaCollection = $this->mediaRepository
->search($criteria, $event->getContext())
->getEntities();
$mediaArr = $mediaCollection->getElements();
$event->getPagelet()->addExtension('footerThemeConfigMedia', new ArrayStruct($mediaArr));
}
}