custom/plugins/SwkwebInfobar/src/Storefront/Subscriber/FooterSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swkweb\Infobar\Storefront\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  9. use Swkweb\Infobar\Core\Content\InfobarEntry\InfobarEntryCollection;
  10. use Swkweb\Infobar\Struct\Infobar;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class FooterSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $systemConfigService;
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $infobarEntryRepository;
  22.     public function __construct(
  23.         SystemConfigService $systemConfigService,
  24.         EntityRepositoryInterface $infobarEntryRepository
  25.     ) {
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->infobarEntryRepository $infobarEntryRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             FooterPageletLoadedEvent::class => 'onFooterPageletLoaded',
  33.         ];
  34.     }
  35.     public function onFooterPageletLoaded(FooterPageletLoadedEvent $event): void
  36.     {
  37.         $criteria = (new Criteria())
  38.             ->addFilter(
  39.                 new EqualsFilter(
  40.                     'salesChannelId',
  41.                     $event->getSalesChannelContext()->getSalesChannel()->getId()
  42.                 )
  43.             )
  44.             ->addSorting(new FieldSorting('position'))
  45.         ;
  46.         $result $this->infobarEntryRepository->search($criteria$event->getContext());
  47.         if (!$result->getTotal()) {
  48.             return;
  49.         }
  50.         /** @var InfobarEntryCollection $entries */
  51.         $entries $result->getEntities();
  52.         $infobar = new Infobar();
  53.         $infobar->setPosition($this->systemConfigService->getString('SwkwebInfobar.config.position'));
  54.         $infobar->setMobile($this->systemConfigService->getBool('SwkwebInfobar.config.mobile'));
  55.         $infobar->setMoreHover($this->systemConfigService->getBool('SwkwebInfobar.config.moreHover'));
  56.         $infobar->setClose($this->systemConfigService->getBool('SwkwebInfobar.config.close'));
  57.         $infobar->setEntries($entries);
  58.         $event->getPagelet()->addExtension('swkwebInfobar'$infobar);
  59.     }
  60. }