custom/plugins/zenitPlatformStratus/src/Subscriber/StorefrontRenderSubscriber.php line 109

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformStratus\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayEntity;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Storefront\Theme\ThemeConfigValueAccessor;
  10. use Shopware\Storefront\Event\StorefrontRenderEvent;
  11. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  12. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  13. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  14. use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
  15. class StorefrontRenderSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var string
  19.      */
  20.     private $pluginName 'zenitPlatformStratus';
  21.     /**
  22.      * @var ThemeConfigValueAccessor
  23.      */
  24.     private $themeConfigAccessor;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $themeRepository;
  29.     /**
  30.      * @var string
  31.      */
  32.     private $themeId;
  33.     public function __construct(ThemeConfigValueAccessor $themeConfigAccessorEntityRepositoryInterface $themeRepository)
  34.     {
  35.         $this->themeConfigAccessor $themeConfigAccessor;
  36.         $this->themeRepository $themeRepository;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  41.         return [
  42.             StorefrontRenderEvent::class => 'onStorefrontRender',
  43.             ProductSuggestCriteriaEvent::class => 'handleSuggestRequest',
  44.             ProductListingCriteriaEvent::class => 'handleListingRequest',
  45.             ProductSearchCriteriaEvent::class => 'handleSearchRequest',
  46.             WishListPageProductCriteriaEvent::class => 'handleWishlistRequest'
  47.         ];
  48.     }
  49.     public function onStorefrontRender(StorefrontRenderEvent $event)
  50.     {
  51.         $config = [];
  52.         $config['system'] = 'platform';
  53.         $event->getContext()->addExtension($this->pluginName, new ArrayEntity($config));
  54.     }
  55.     public function handleListingRequest(ProductListingCriteriaEvent $event)
  56.     {
  57.         $context $event->getContext();
  58.         $themeId $this->getThemeIdByTechnicalName($this->pluginName$context);
  59.         $this->themeId $themeId;
  60.         $salesChannelContext $event->getSalesChannelContext();
  61.         $cardImgSwitch $this->themeConfigAccessor->get('zen-product-listing-card-img-switch'$salesChannelContext$this->themeId );
  62.         if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
  63.             $criteria $event->getCriteria();
  64.             $criteria->addAssociation('media');
  65.         }
  66.     }
  67.     public function handleSuggestRequest(ProductSuggestCriteriaEvent $event)
  68.     {
  69.         $context $event->getContext();
  70.         $themeId $this->getThemeIdByTechnicalName($this->pluginName$context);
  71.         $this->themeId $themeId;
  72.         $salesChannelContext $event->getSalesChannelContext();
  73.         $cardImgSwitch $this->themeConfigAccessor->get('zen-product-listing-card-img-switch'$salesChannelContext$this->themeId );
  74.         if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
  75.             $criteria $event->getCriteria();
  76.             $criteria->addAssociation('media');
  77.         }
  78.     }
  79.     public function handleSearchRequest(ProductSearchCriteriaEvent $event)
  80.     {
  81.         $context $event->getContext();
  82.         $themeId $this->getThemeIdByTechnicalName($this->pluginName$context);
  83.         $this->themeId $themeId;
  84.         $salesChannelContext $event->getSalesChannelContext();
  85.         $cardImgSwitch $this->themeConfigAccessor->get('zen-product-listing-card-img-switch'$salesChannelContext$this->themeId );
  86.         if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
  87.             $criteria $event->getCriteria();
  88.             $criteria->addAssociation('media');
  89.         }
  90.     }
  91.     public function handleWishlistRequest(WishListPageProductCriteriaEvent $event)
  92.     {
  93.         $context $event->getContext();
  94.         $themeId $this->getThemeIdByTechnicalName($this->pluginName$context);
  95.         $this->themeId $themeId;
  96.         $salesChannelContext $event->getSalesChannelContext();
  97.         $cardImgSwitch $this->themeConfigAccessor->get('zen-product-listing-card-img-switch'$salesChannelContext$this->themeId );
  98.         if( isset($cardImgSwitch) && $cardImgSwitch !== 'none' ) {
  99.             $criteria $event->getCriteria();
  100.             $criteria->addAssociation('media');
  101.         }
  102.     }
  103.     private function getThemeIdByTechnicalName(string $technicalNameContext $context): ?string
  104.     {
  105.         return $this->themeRepository->searchIds(
  106.             (new Criteria())->addFilter(new EqualsFilter('technicalName'$technicalName)),
  107.             $context
  108.         )->firstId();
  109.     }
  110. }