custom/plugins/ZweiPunktCurchod/src/Subscriber/AddPdfInfosToView.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ZweiPunktCurchod\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9.  * class AddPdfInfosToView
  10.  *
  11.  * Determines the PDF information stored on the product
  12.  */
  13. class AddPdfInfosToView implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private EntityRepositoryInterface $mediaRepo;
  19.     /**
  20.      * @param EntityRepositoryInterface $mediaRepo
  21.      */
  22.     public function __construct(
  23.         EntityRepositoryInterface $mediaRepo
  24.     ) {
  25.         $this->mediaRepo $mediaRepo;
  26.     }
  27.     /**
  28.      * @return string[]
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent'
  34.         ];
  35.     }
  36.     /**
  37.      * @param ProductPageLoadedEvent $event
  38.      *
  39.      * Determines the PDF information stored on the product
  40.      * from the free text fields and passes it on to view
  41.      */
  42.     public function onProductPageLoadedEvent(
  43.         ProductPageLoadedEvent $event
  44.     ) {
  45.         // Determines the free text fields of the product
  46.         $customFields $event->getPage()->getProduct()->getCustomFields();
  47.         // If there are no free text fields, nothing else needs to be done
  48.         if (empty($customFields)) {
  49.             return;
  50.         }
  51.         // Determines the pdf ids stored in the free text fields if any are available
  52.         $pdfFields = [];
  53.         foreach ($customFields as $index => $field) {
  54.             if (str_contains($index'custom_datenblatt_pdf_1')) {
  55.                 $pdfFields[] = $field;
  56.             }
  57.         }
  58.         // If no Ids were found, it is not necessary to search for a media file.
  59.         if (empty($pdfFields)) {
  60.             return;
  61.         }
  62.         // The search criteria are set.
  63.         // All entries are to be determined whose id are contained in the pdfFields array.
  64.         $criteria = new Criteria();
  65.         $criteria->addFilter(
  66.             new EqualsAnyFilter('id'$pdfFields)
  67.         );
  68.         // The media information is searched
  69.         $medias $this->mediaRepo->search($criteria$event->getContext());
  70.         // If no media information is found,
  71.         // nothing needs to be passed to the view.
  72.         if (== $medias->getTotal()) {
  73.             return;
  74.         }
  75.         // The url and the name of the media are determined
  76.         $data = [];
  77.         foreach ($medias as $index => $media) {
  78.             $data[$index] = [
  79.                 'url' => $media->getUrl(),
  80.                 'name' => $media->getFileName()
  81.             ];
  82.         }
  83.         // The required media data are passed to the view and can be found under page.pdfInformations
  84.         $event->getPage()->assign(['pdfInformations' => $data]);
  85.     }
  86. }