custom/plugins/LoyxxRichBreadcrumb/src/Subscriber/ProductPageLoadedEvent.php line 79

Open in your IDE?
  1. <?php
  2. namespace LoyxxRichBreadcrumb\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use LoyxxRichBreadcrumb\Struct\Breadcrumb;
  5. use LoyxxRichBreadcrumb\Struct\Data;
  6. use LoyxxRichBreadcrumb\Traits\Microdata;
  7. use Shopware\Core\Content\Seo\SeoUrl\SeoUrlEntity;
  8. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * Class ProductPageLoadedEvent
  17.  * @package LoyxxRichBreadcrumb\Subscriber
  18.  */
  19. class ProductPageLoadedEvent implements EventSubscriberInterface
  20. {
  21.     use Microdata;
  22.     /**
  23.      * @var SeoUrlPlaceholderHandlerInterface
  24.      */
  25.     private $seoUrlPlaceholderHandler;
  26.     /**
  27.      * @var SystemConfigService
  28.      */
  29.     private $systemConfigService;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private $seoUrlRepository;
  34.     /**
  35.      * @var Connection
  36.      */
  37.     private $connection;
  38.     /**
  39.      * ProductPageLoadedEvent constructor.
  40.      * @param SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler
  41.      * @param SystemConfigService $systemConfigService
  42.      * @param EntityRepositoryInterface $seoUrlRepository
  43.      * @param Connection $connection
  44.      */
  45.     public function __construct(
  46.         SeoUrlPlaceholderHandlerInterface $seoUrlPlaceholderHandler,
  47.         SystemConfigService $systemConfigService,
  48.         EntityRepositoryInterface $seoUrlRepository,
  49.         Connection $connection
  50.     )
  51.     {
  52.         $this->seoUrlPlaceholderHandler $seoUrlPlaceholderHandler;
  53.         $this->systemConfigService $systemConfigService;
  54.         $this->seoUrlRepository $seoUrlRepository;
  55.         $this->connection $connection;
  56.     }
  57.     /**
  58.      * @return array|string[]
  59.      */
  60.     public static function getSubscribedEvents()
  61.     {
  62.         return [
  63.             \Shopware\Storefront\Page\Product\ProductPageLoadedEvent::class => 'onProductPageLoadedEvent'
  64.         ];
  65.     }
  66.     /**
  67.      * @param \Shopware\Storefront\Page\Product\ProductPageLoadedEvent $event
  68.      */
  69.     public function onProductPageLoadedEvent(\Shopware\Storefront\Page\Product\ProductPageLoadedEvent $event)
  70.     {
  71.         // create microdata for breadcrumb
  72.         $host $event->getRequest()->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL) .
  73.             $event->getRequest()->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL);
  74.         $uri $event->getRequest()->getRequestUri();
  75.         if ( $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.modifyProductDetailBreadcrumb')){
  76.             $this->checkAndUpdateSeoBreadcrumbNavigation($event$host);
  77.         }
  78.         $navigationTree $event->getPage()->getHeader()->getNavigation()->getTree();
  79.         $product $event->getPage()->getProduct();
  80.         $productBreadcrumb = [];
  81.         $this->createBreadcrumbTree($navigationTree$product$productBreadcrumb);
  82.         /**
  83.          * Hide breadcrumb on home page
  84.          *
  85.          * $breadcrumbIsVisible = $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME.'.config.isInvisible');
  86.          * if ($this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME . '.config.isHiddenOnHomePage') && $event->getRequest()->attributes->get('_route') === 'frontend.home.page'){
  87.          * $breadcrumbIsVisible = false;
  88.          * }
  89.          *  */
  90.         $breadcrumbStruct = new Breadcrumb(
  91.             $this->createMicrodata(
  92.                 $event,
  93.                 $productBreadcrumb,
  94.                 $host,
  95.                 $event->getSalesChannelContext(),
  96.                 $uri
  97.             ),
  98.             $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.isInvisible'),
  99.             $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.showProductTitle'),
  100.             $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.separator'),
  101.             $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.isVisibleHomeLink'),
  102.             $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.isHiddenOnHomePage'),
  103.             $this->systemConfigService->get(PageLoadedEventSubscriber::BUNDLE_NAME '.config.homeLabel'),
  104.             $host,
  105.             $uri
  106.         );
  107.         $event->getPage()->addExtension('breadcrumb'$breadcrumbStruct);
  108.         $event->getPage()->getProduct()->addExtension('breadcrumb'$breadcrumbStruct);
  109.     }
  110.     private function checkAndUpdateSeoBreadcrumbNavigation(\Shopware\Storefront\Page\Product\ProductPageLoadedEvent $eventstring $host)
  111.     {
  112.         $referer $event->getRequest()->server->get('HTTP_REFERER');
  113.         if ($referer && $referer !== $host) {
  114.             $referer str_replace($host""$referer);
  115.             // remove ? and # from fragment from the URI
  116.             $referer ltrim(substr(substr($referer0strrpos($referer'?') ?: strlen($referer)), 0strrpos($referer'#') ?: strlen($referer)), "/");
  117.             if ($referer){
  118.                 // get the navigation id if it present in the seoUrl page
  119.                 $criteria = new Criteria();
  120.                 $criteria->setLimit(1);
  121.                 $criteria->setTotalCountMode(1);
  122.                 $criteria->addFilter(new ContainsFilter('seoPathInfo'"$referer"));
  123.                 /** @var SeoUrlEntity $seoUrl */
  124.                 $seoUrl $this->seoUrlRepository->search($criteria$event->getContext())->first();
  125.                 if(is_null($seoUrl)){
  126.                     // the received $lastPart was a uuid
  127.                     $criteria = new Criteria();
  128.                     $criteria->setLimit(1);
  129.                     $criteria->setTotalCountMode(1);
  130.                     $criteria->addFilter(new ContainsFilter('pathInfo'$referer));
  131.                     $seoUrl $this->seoUrlRepository->search($criteria$event->getContext())->first();
  132.                 }
  133.                 if ($seoUrl){
  134.                     $categoryId $seoUrl->getForeignKey();
  135.                     // check existence of the category
  136.                     $rows $this->connection->executeQuery("SELECT count(id) as row_count FROM category WHERE hex(id) = ?", [$categoryId]);
  137.                     $row $rows->fetch(\PDO::FETCH_ASSOC);
  138.                     if (isset($row['row_count']) && $row['row_count']){
  139.                         $event->getPage()->getProduct()->addExtension('seoBreadcrumbActiveCategory', new Data(['id' => $categoryId]));
  140.                     }
  141.                 }
  142.             }
  143.         }
  144.     }
  145.     /**
  146.      * @param array $navigationTree
  147.      * @param \Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity $product
  148.      * @param $productBreadcrumb
  149.      */
  150.     private function createBreadcrumbTree(array $navigationTree, \Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity $product, &$productBreadcrumb)
  151.     {
  152.         if ($navigationTree) {
  153.             $productCategoryTree $product->getCategoryTree();
  154.             if ($productCategoryTree){
  155.                 $lastProductCategoryTree end($productCategoryTree);
  156.                 foreach ($navigationTree as $item) {
  157.                     if ($item->getCategory()->getId() == $lastProductCategoryTree) {
  158.                         $breadcrumb $item->getCategory()->getPlainBreadcrumb($item->getCategory()->getId());
  159.                         $breadcrumb array_slice($breadcrumb1);
  160.                         if ($breadcrumb) {
  161.                             foreach ($breadcrumb as $key => $item) {
  162.                                 $productBreadcrumb[$key] = $item;
  163.                             }
  164.                         }
  165.                     } else if ($item->getChildren()) {
  166.                         $this->createBreadcrumbTree($item->getChildren(), $product$productBreadcrumb);
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.     }
  172. }