custom/plugins/SemesManufacturerPlus/src/Storefront/Subscriber/ProductDetailSubscriber.php line 176

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SemesManufacturerPlus\Storefront\Subscriber;
  3. use Symfony\Component\HttpKernel\KernelEvents;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\Struct\ArrayStruct;
  7. use Shopware\Core\Framework\Adapter\Cache\CacheInvalidator;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  12. use Shopware\Core\Content\Product\Events\ProductDetailRouteCacheTagsEvent;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  16. use SemesManufacturerPlus\Service\ConfigService;
  17. use SemesManufacturerPlus\Service\UrlService;
  18. use SemesManufacturerPlus\Service\ManufacturerService;
  19. class ProductDetailSubscriber implements EventSubscriberInterface
  20. {
  21.     private $configService;
  22.     private $systemConfigService;
  23.     private $cacheInvalidator;
  24.     private $breadcrumbs;
  25.     private $urlService;
  26.     private $manufacturerService;
  27.     public function __construct(
  28.         ConfigService $configService,
  29.         SystemConfigService $systemConfigService,
  30.         CacheInvalidator $cacheInvalidator,
  31.         UrlService $urlService,
  32.         ManufacturerService $manufacturerService
  33.     ) {
  34.         $this->configService $configService;
  35.         $this->systemConfigService $systemConfigService;
  36.         $this->cacheInvalidator $cacheInvalidator;
  37.         $this->urlService $urlService;
  38.         $this->manufacturerService $manufacturerService;
  39.     }
  40.     /**
  41.      * @return array 
  42.      */
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  47.             // ProductSearchCriteriaEvent::class => 'onProductSearchCriteria',
  48.             ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  49.             ProductDetailRouteCacheTagsEvent::class => 'onProductDetailRouteCacheTagsEvent',
  50.             //KernelEvents::CONTROLLER => 'invalidateCache',
  51.             KernelEvents::REQUEST => 'invalidateCache'
  52.         ];
  53.     }
  54.     /**
  55.      * 
  56.      * @param ProductDetailRouteCacheTagsEvent $event 
  57.      * @return void 
  58.      * @throws InconsistentCriteriaIdsException 
  59.      */
  60.     public function onProductDetailRouteCacheTagsEvent($event)
  61.     {
  62.         $event->addTags(['referer-category']);
  63.     }
  64.     /**
  65.      * 
  66.      * @param ProductPageLoadedEvent $event 
  67.      * @return void 
  68.      * @throws InconsistentCriteriaIdsException 
  69.      */
  70.     public function onProductPageLoaded($event)
  71.     {
  72.         $request $event->getRequest();
  73.         $page $event->getPage();
  74.         $salesChannelContext $event->getSalesChannelContext();
  75.         $salesChannel $salesChannelContext->getSalesChannel();
  76.         // get product categories
  77.         $product $page->getProduct();
  78.         if (!$product) return;
  79.         $data = new ArrayStruct();
  80.         if ($manufacturerBreadcrumbs $this->getManufacturerBreadcrumbs($request$salesChannelContext->getContext(), $salesChannel)) {
  81.             $data->set('breadcrumbs'$manufacturerBreadcrumbs['breadcrumbs']);
  82.         }
  83.         if ($productManufacturer $product->getManufacturer()) {
  84.             $manufacturerLink $this->urlService->generateFullManufacturerLink($productManufacturer$request$salesChannelContext->getContext());
  85.             $data->set('manufacturerLink'$manufacturerLink);
  86.         }
  87.         if(!empty($data->all())) {
  88.             $page->getHeader()->addExtension('semesManufacturerProduct'$data);
  89.         }
  90.         
  91.         return;
  92.     }
  93.     /**
  94.      * 
  95.      * @param ProductPageCriteriaEvent $event 
  96.      * @return void 
  97.      */
  98.     public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
  99.     {
  100.         $criteria $event->getCriteria();
  101.         $criteria->addAssociation('manufacturer.extensions.manufacturerPlus.bannerImageMedia');
  102.         $criteria->addAssociation('manufacturer.extensions.manufacturerPlus.smallLogoMedia');
  103.     }
  104.     /**
  105.      * 
  106.      * @param ProductSearchCriteriaEvent $event 
  107.      * @return void 
  108.      */
  109.     public function onProductSearchCriteria(ProductSearchCriteriaEvent $event): void
  110.     {
  111.         $criteria $event->getCriteria();
  112.         $criteria->addAssociation('manufacturer.extensions.manufacturerPlus');
  113.         $criteria->addAssociation('manufacturer.extensions.manufacturerPlus.manufacturerPlus.bannerImageMedia');
  114.         $criteria->addAssociation('manufacturer.extensions.manufacturerPlus.manufacturerPlus.smallLogoMedia');
  115.     }
  116.     /**
  117.      * getManufacturerBreadcrumbs 
  118.      * - function checks if a referer is set
  119.      * - then the breadcrumbs are generate by the urlService
  120.      * 
  121.      * return array of breadcrumbs to be used within twig template
  122.      */
  123.     private function getManufacturerBreadcrumbs($requestContext $context$salesChannel)
  124.     {
  125.         if (!is_null($this->breadcrumbs)) return  $this->breadcrumbs;
  126.         $url $_SERVER['HTTP_REFERER'] ?? '';
  127.         foreach ($salesChannel->getDomains() as $domain) {
  128.             $url stripos($url$domain->getUrl()) === ? (str_replace($domain->getUrl(), ''$url)) : $url;
  129.         }
  130.         if (!$this->urlService->isManufacturerUrl($url$context)) return;
  131.         $url explode('?'$url2);
  132.         $url $url[0];
  133.         $url trim(strtolower($url), '/');
  134.         if ($this->configService->getTrailingSlash()) {
  135.             $url $url '/';
  136.         }
  137.         $indexBreadcrumb $this->urlService->getIndexBreadcrumb($request$context);
  138.         $manufacturer $this->manufacturerService->getManufacturerByUrl($url$context);
  139.         $manufacturerBreadcrumb $this->urlService->getManufacturerBreadcrumb($manufacturer$request$context);
  140.         $this->breadcrumbs['breadcrumbs'] = [
  141.             $indexBreadcrumb,
  142.             $manufacturerBreadcrumb
  143.         ];
  144.         return  $this->breadcrumbs;
  145.     }
  146.     public function invalidateCache()
  147.     {
  148.         $this->cacheInvalidator->invalidate(['referer-category']);
  149.     }
  150. }