custom/plugins/DreiscSeoPro/src/Subscriber/Installment/Canonical/CanonicalSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Subscriber\Installment\Canonical;
  3. use DreiscSeoPro\Core\Canonical\CanonicalFetcher;
  4. use DreiscSeoPro\Core\Canonical\CanonicalFetcherStruct;
  5. use DreiscSeoPro\Core\Content\Product\ProductEnum;
  6. use DreiscSeoPro\Core\Seo\SeoDataFetcher\ProductSeoDataFetcher;
  7. use Shopware\Core\Content\Category\CategoryDefinition;
  8. use Shopware\Core\Content\Product\ProductDefinition;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  11. use Shopware\Core\SalesChannelRequest;
  12. use Shopware\Storefront\Page\Navigation\NavigationPage;
  13. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  14. use Shopware\Storefront\Page\Page;
  15. use Shopware\Storefront\Page\PageLoadedEvent;
  16. use Shopware\Storefront\Page\Product\ProductPage;
  17. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class CanonicalSubscriber implements EventSubscriberInterface
  20. {
  21.     public const DREISC_SEO_INSTALLMENT_CANONICAL_DATA 'dreiscSeoInstallmentCanonicalData';
  22.     /**
  23.      * @var CanonicalFetcher
  24.      */
  25.     private $canonicalFetcher;
  26.     /**
  27.      * @param CanonicalFetcher $canonicalFetcher
  28.      */
  29.     public function __construct(CanonicalFetcher $canonicalFetcher)
  30.     {
  31.         $this->canonicalFetcher $canonicalFetcher;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             ProductPageLoadedEvent::class => 'addInstallment',
  37.             NavigationPageLoadedEvent::class => 'addInstallment'
  38.         ];
  39.     }
  40.     /**
  41.      * @param PageLoadedEvent $event
  42.      * @throws InconsistentCriteriaIdsException
  43.      */
  44.     public function addInstallment(PageLoadedEvent $event)
  45.     {
  46.         /** @var Page|ProductPage|NavigationPage $page */
  47.         $page $event->getPage();
  48.         $salesChannelEntity $event->getSalesChannelContext()->getSalesChannel();
  49.         $salesChannelDomainId $event->getRequest()->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_ID);
  50.         $languageId $event->getContext()->getLanguageId();
  51.         if ($page instanceof ProductPage) {
  52.             $canonicalLink $this->canonicalFetcher->fetch(
  53.                 new CanonicalFetcherStruct(
  54.                     ProductDefinition::ENTITY_NAME,
  55.                     $page->getProduct()->getId(),
  56.                     $languageId,
  57.                     $salesChannelEntity->getId(),
  58.                     $salesChannelDomainId
  59.                 )
  60.             );
  61.         } elseif ($page instanceof NavigationPage) {
  62.             /** Try to fetch the active category */
  63.             $categoryId $event->getRequest()->attributes->get('navigationId');
  64.             /** Fetch the home category id */
  65.             if(
  66.                 empty($categoryId) &&
  67.                 'frontend.home.page' === $event->getRequest()->attributes->get('_route') &&
  68.                 null !== $page->getHeader() &&
  69.                 null !== $page->getHeader()->getNavigation()
  70.             ) {
  71.                 $activeCategory $page->getHeader()->getNavigation()->getActive();
  72.                 if (null !== $activeCategory) {
  73.                     $categoryId $activeCategory->getId();
  74.                 }
  75.             }
  76.             /** Abort, if category is null */
  77.             if (null === $categoryId) {
  78.                 return;
  79.             }
  80.             $canonicalLink $this->canonicalFetcher->fetch(
  81.                 new CanonicalFetcherStruct(
  82.                     CategoryDefinition::ENTITY_NAME,
  83.                     $categoryId,
  84.                     $languageId,
  85.                     $salesChannelEntity->getId(),
  86.                     $salesChannelDomainId
  87.                 )
  88.             );
  89.         }
  90.         if (null !== $canonicalLink) {
  91.             $page->addExtension(
  92.                 self::DREISC_SEO_INSTALLMENT_CANONICAL_DATA,
  93.                 new CanonicalDataStruct($canonicalLink)
  94.             );
  95.         }
  96.     }
  97. }