custom/plugins/MoorlFoundation/src/Storefront/Subscriber/SalesChannelContextResolvedSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFoundation\Storefront\Subscriber;
  3. use Shopware\Core\Content\Cms\CmsPageEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class SalesChannelContextResolvedSubscriber implements EventSubscriberInterface
  10. {
  11.     private SystemConfigService $systemConfigService;
  12.     private EntityRepositoryInterface $cmsPageRepository;
  13.     public function __construct(
  14.         SystemConfigService $systemConfigService,
  15.         EntityRepositoryInterface $cmsPageRepository
  16.     )
  17.     {
  18.         $this->systemConfigService $systemConfigService;
  19.         $this->cmsPageRepository $cmsPageRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolvedEvent',
  25.         ];
  26.     }
  27.     public function onSalesChannelContextResolvedEvent(SalesChannelContextResolvedEvent $event): void
  28.     {
  29.         return;
  30.         $salesChannelContext $event->getSalesChannelContext();
  31.         $salesChannelId $salesChannelContext->getSalesChannelId();
  32.         $moorlFoundationListingConfig $this->systemConfigService->get(
  33.             'MoorlFoundation.config.moorlFoundationListingConfig',
  34.             $salesChannelId
  35.         );
  36.         if (!$moorlFoundationListingConfig) {
  37.             return;
  38.         }
  39.         $criteria = new Criteria([$moorlFoundationListingConfig]);
  40.         $criteria->setLimit(1);
  41.         $criteria->addAssociation('sections.blocks.slots');
  42.         /** @var CmsPageEntity $cmsPage */
  43.         $cmsPage $this->cmsPageRepository->search($criteria$salesChannelContext->getContext())->get($moorlFoundationListingConfig);
  44.         if (!$cmsPage) {
  45.             return;
  46.         }
  47.         foreach ($cmsPage->getSections() as $section) {
  48.             foreach ($section->getBlocks() as $block) {
  49.                 foreach ($block->getSlots() as $slot) {
  50.                     $config $slot->getConfig();
  51.                     if (!empty($config['listingLayout'])) {
  52.                         $salesChannelContext->assign(['moorlFoundationListingConfig' => $config]);
  53.                         return;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }