vendor/crehler/product-files/src/Subscriber/ProductSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\ProductFiles\Subscriber;
  3. use Crehler\ProductFiles\Service\ProductFilesService;
  4. use Shopware\Storefront\Page\Product\ProductLoaderCriteriaEvent;
  5. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ProductSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var ProductFilesService
  12.      */
  13.     private $productFilesService;
  14.     public function __construct(ProductFilesService $productFilesService)
  15.     {
  16.         $this->productFilesService $productFilesService;
  17.     }
  18.     /**
  19.      * @return array
  20.      */
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductPageCriteriaEvent::class => 'onProductLoaderCriteria',
  25.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  26.         ];
  27.     }
  28.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  29.     {
  30.         $page $event->getPage();
  31.         $product $page->getProduct();
  32.         $context $event->getSalesChannelContext();
  33.         $productFiles $this->productFilesService->prepareProductFiles($product$context);
  34.         $product->addExtension('productFiles'$productFiles);
  35.     }
  36.     /**
  37.      * @param ProductLoaderCriteriaEvent $event
  38.      */
  39.     public function onProductLoaderCriteria(ProductPageCriteriaEvent $event)
  40.     {
  41.         try {
  42.             $event
  43.                 ->getCriteria()
  44.                 ->addAssociation('downloadFiles')
  45.                 ->addAssociation('downloadFilesLinks');
  46.         } catch (\Exception $e) {
  47.         }
  48.     }
  49. }