custom/plugins/NimbitsArticlePDFsNext/src/Subscriber/Subscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nimbits\NimbitsArticlePDFsNext\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Content\Product\ProductEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryForwardCompatibilityDecorator;
  9. use Shopware\Core\Content\Cms\CmsPageEntity;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\HttpKernel\Event\KernelEvent;
  12. use Shopware\Storefront\Event\StorefrontRenderEvent;
  13. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  14. use Nimbits\NimbitsArticlePDFsNext\Setting\Service\SettingService;
  15. use Shopware\Core\Content\Media\DataAbstractionLayer\MediaRepositoryDecorator;
  16. use Shopware\Core\Framework\Struct\ArrayEntity;
  17. class Subscriber implements EventSubscriberInterface
  18. {
  19.     
  20.     /** @var SettingService $settingsService */
  21.     protected $settingsService;
  22.     
  23.     /** @var EntityRepositoryForwardCompatibilityDecorator $mediaRepository */
  24.     protected $mediaRepository;
  25.     public function __construct(
  26.         SettingService $settingsService,
  27.         EntityRepositoryForwardCompatibilityDecorator $mediaRepository
  28.     
  29.     {
  30.         $this->settingsService $settingsService;
  31.         $this->mediaRepository $mediaRepository;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  36.         return [
  37.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  38.         ];
  39.     }
  40.     
  41.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  42.     {
  43.         $customFields $event->getPage()->getProduct()->getCustomFields();
  44.         $ids = [];
  45.         
  46.         for($i 1$i <= 10$i++)
  47.         {
  48.             if(!isset($customFields['custom_nimbits_pdftab'.$i.'_media']))
  49.                 continue;
  50.             
  51.             $ids[] = $customFields['custom_nimbits_pdftab'.$i.'_media'];
  52.         }
  53.         
  54.         if(!empty($ids)){
  55.         
  56.             /** @var EntityCollection $entities */
  57.             $mediaFiles $this->mediaRepository->search(
  58.                 new Criteria($ids),
  59.                 \Shopware\Core\Framework\Context::createDefaultContext()
  60.             );
  61.             $event->getPage()->getProduct()->addExtension('articlepdfs', new ArrayEntity($mediaFiles->getElements()));
  62.         }
  63.         else{
  64.             $event->getPage()->getProduct()->addExtension('articlepdfs', new ArrayEntity([]));    
  65.         }
  66.         $event->getPage()->getProduct()->addExtension('nimbitsArticlePdfSettings'$this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId()));
  67.     
  68.     
  69.     }
  70.     
  71. }
  72. ?>