<?php
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\MojeBambinoEngine\Struct\FooterIconsGroupsStruct;
use Crehler\MojeBambinoEngine\Struct\FooterIconsStruct;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FooterPageletSubscriber implements EventSubscriberInterface
{
/** @var EntityRepository */
private $iconsRepository;
/** @var EntityRepository */
private $visibilityRepository;
public function __construct(EntityRepository $iconsRepository, EntityRepository $visibilityRepository)
{
$this->iconsRepository = $iconsRepository;
$this->visibilityRepository = $visibilityRepository;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'addFooterIcons',
];
}
public function addFooterIcons(FooterPageletLoadedEvent $event): void
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('salesChannelId', $event->getSalesChannelContext()->getSalesChannel()->getId()))
->addAssociation('icon');
/** @var \Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult $ids */
$entities = $this->visibilityRepository->search($criteria, $event->getContext());
//dd($entities);
$iconsGroups = new FooterIconsGroupsStruct();
/** @var ArrayEntity $entity */
foreach ($entities as $entity) {
$iconEntity = $entity->get('icon');
if ($iconEntity === null) continue;
$type = $iconEntity->get('type');
$position = (int)$iconEntity->get('position');
$icon = (new FooterIconsStruct())
->setName(($iconEntity->getTranslation('name') !== null) ? $iconEntity->getTranslation('name') : $iconEntity->get('name'))
->setAlt(($iconEntity->getTranslation('alt') !== null) ? $iconEntity->getTranslation('alt') : $iconEntity->get('alt'))
->setUrl(($iconEntity->getTranslation('url') !== null) ? $iconEntity->getTranslation('url') : $iconEntity->get('url'))
->setMedia($iconEntity->get('media'))
->setTarget($iconEntity->get('target'));
if (!empty($iconEntity->get('rel'))) $icon->setRel($iconEntity->get('rel'));
switch ($type) {
case 'social':
$iconsGroups->addSocial($icon, $position);
break;
case 'brands':
$iconsGroups->addBrands($icon, $position);
break;
}
}
$iconsGroups->sort();
$event->getPagelet()->addExtension('footer_icons', $iconsGroups);
}
}