<?php
namespace GrimmTheme\Storefront\Subscriber;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductDetailSubscriber implements EventSubscriberInterface
{
private static array $mimeTypeFilter = [
'application/pdf'
];
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductDetailPage'
];
}
/**
* Remove pdf files from product detail page
*
* @param ProductPageLoadedEvent $productPageLoadedEvent
*/
public function onProductDetailPage(ProductPageLoadedEvent $productPageLoadedEvent)
{
$product = $productPageLoadedEvent->getPage()->getProduct();
$mediaCollection = $product->getMedia();
$downloads = [];
foreach ($mediaCollection as $key => $mediaEntity) {
if(in_array($mediaEntity->getMedia()->getMimeType(), self::$mimeTypeFilter)) {
$mediaCollection->remove($key);
$downloads[] = $mediaEntity;
}
}
if($mediaCollection) {
$product->setMedia($mediaCollection);
}
if(!empty($downloads)) {
$customFields = $product->getCustomFields();
$customFields['downloads'] = $downloads;
$product->setCustomFields($customFields);
}
}
}