custom/plugins/AcrisProductVideoCS/src/Storefront/Subscriber/ProductPageSubscriber.php line 44

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductVideo\Storefront\Subscriber;
  3. use Acris\ProductVideo\Components\Service\ProductVideoService;
  4. use Acris\ProductVideo\Custom\ProductVideoCollection;
  5. use Acris\ProductVideo\Custom\ProductVideoEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductPageSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var SystemConfigService
  15.      */
  16.     private $systemConfigService;
  17.     /**
  18.      * @var ProductVideoService
  19.      */
  20.     private $productVideoService;
  21.     public function __construct(
  22.         SystemConfigService $systemConfigService,
  23.         ProductVideoService $productVideoService
  24.     ) {
  25.         $this->systemConfigService $systemConfigService;
  26.         $this->productVideoService $productVideoService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductPageCriteriaEvent::class => 'onProductCriteriaLoaded',
  32.             ProductPageLoadedEvent::class => [
  33.                 ['onProductPageLoaded'200]
  34.             ]
  35.         ];
  36.     }
  37.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  38.     {
  39.         if($event->getPage()->getProduct()->hasExtension('acrisVideos') !== true) {
  40.             return;
  41.         }
  42.         /** @var ProductVideoCollection $productVideoCollection */
  43.         $productVideoCollection $event->getPage()->getProduct()->getExtension('acrisVideos');
  44.         if ($productVideoCollection->count() === 0) return;
  45.         /** @var ProductVideoEntity $productVideo */
  46.         foreach ($productVideoCollection->getElements() as $productVideo)
  47.         {
  48.             if (!empty($productVideo) && !empty($productVideo->getTranslation('link')) && ($productVideo->getType() === ProductVideoService::DEFAULT_PRODUCT_VIDEO_YOUTUBE_TYPE || $productVideo->getType() === ProductVideoService::DEFAULT_PRODUCT_VIDEO_VIMEO_TYPE)) {
  49.                 $this->productVideoService->loadProductVideo($productVideo$event->getSalesChannelContext());
  50.             }
  51.         }
  52.         $this->productVideoService->assignPreviewImage($productVideoCollection$event->getSalesChannelContext());
  53.         $this->productVideoService->assignProductVideoToMedia($event->getPage()->getProduct(), $productVideoCollection$event->getSalesChannelContext());
  54.     }
  55.     public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
  56.     {
  57.         if(empty($event->getSalesChannelContext()->getSalesChannel())) {
  58.             return;
  59.         }
  60.         $sorting $this->getVideoSorting($event->getSalesChannelContext()->getSalesChannel()->getId());
  61.         $event->getCriteria()->addAssociation('acrisVideos');
  62.         $event->getCriteria()->addAssociation('acrisVideos.media');
  63.         $event->getCriteria()->getAssociation('acrisVideos')->addSorting(new FieldSorting('priority'$sortingtrue));
  64.     }
  65.     private function getVideoSorting(string $salesChannelId): string
  66.     {
  67.         if($this->systemConfigService->get('AcrisProductVideoCS.config.highestPriorityFirst'$salesChannelId) === true) {
  68.             return FieldSorting::DESCENDING;
  69.         } else {
  70.             return FieldSorting::ASCENDING;
  71.         }
  72.     }
  73. }