custom/plugins/econsorsetup/src/Subscriber/EconsorSetupRobotMetaSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace EconsorSetup\Subscriber;
  4. use Exception;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  9. use Shopware\Storefront\Page\Page;
  10. use Shopware\Storefront\Page\PageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class EconsorSetupRobotMetaSubscriber implements EventSubscriberInterface
  14. {
  15.     private EntityRepository $productRepository;
  16.     private string $devInstalled;
  17.     public function __construct(EntityRepository $productRepositorystring $devInstalled)
  18.     {
  19.         $this->productRepository $productRepository;
  20.         $this->devInstalled $devInstalled;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             GenericPageLoadedEvent::class => 'onPageLoaded',
  26.         ];
  27.     }
  28.     public function onPageLoaded(PageLoadedEvent $event): void
  29.     {
  30.         try {
  31.             // check for Customfields, if EconsorDev Plugin is NOT active via .xml Parameter.
  32.             $request $event->getRequest();
  33.             if (!$request->isXmlHttpRequest() && "false" === $this->devInstalled) {
  34.                 $route $request->attributes->get("_route");
  35.                 assert(is_string($route));
  36.                 switch ($route) {
  37.                     case 'frontend.home.page':
  38.                         $customFields $this->getHomeCustomFields($event);
  39.                         break;
  40.                     case 'frontend.navigation.page':
  41.                         $customFields $this->getNavigationCustomFields($event);
  42.                         break;
  43.                     case 'frontend.detail.page':
  44.                         $customFields $this->getDetailCustomFields($event$request);
  45.                         break;
  46.                     default:
  47.                         $customFields null;
  48.                 }
  49.                 $this->setRobots($event$customFields);
  50.             }
  51.         } catch (Exception $e) {
  52.             /** TODO: log error */
  53.         }
  54.     }
  55.     private function getHomeCustomFields(PageLoadedEvent $event): ?array
  56.     {
  57.         return $event->getSalesChannelContext()->getSalesChannel()->getCustomFields();
  58.     }
  59.     private function getNavigationCustomFields(PageLoadedEvent $event): ?array
  60.     {
  61.         if (!($page $event->getPage()) instanceof Page) {
  62.             return null;
  63.         }
  64.         if (null === $header $page->getHeader()) {
  65.             return null;
  66.         }
  67.         if (null === $tree $header->getNavigation()) {
  68.             return null;
  69.         }
  70.         if (null === $active $tree->getActive()) {
  71.             return null;
  72.         }
  73.         return $active->getCustomFields();
  74.     }
  75.     private function getDetailCustomFields(PageLoadedEvent $eventRequest $request): ?array
  76.     {
  77.         $productId $request->query->get("productId");
  78.         assert(is_string($productId));
  79.         try {
  80.             $result $this->productRepository->search(
  81.                 (new Criteria([$productId]))->addAssociation('customFields'),
  82.                 $event->getSalesChannelContext()->getContext()
  83.             );
  84.             if ($result->getTotal()) {
  85.                 /** @var ProductEntity $product */
  86.                 $product $result->first();
  87.                 return $product->getCustomFields();
  88.             }
  89.         } catch (Exception $e) {
  90.             /** TODO: log error */
  91.         }
  92.         return null;
  93.     }
  94.     private function setRobots(PageLoadedEvent $event, ?array $customFields): void
  95.     {
  96.         $metaSetting 'index,follow';
  97.         if (isset($customFields)) {
  98.             if (array_key_exists('custom_meta_robots'$customFields)) {
  99.                 if (true === $customFields['custom_meta_robots']) {
  100.                     $metaSetting 'noindex,nofollow';
  101.                 }
  102.             }
  103.         }
  104.         if ($event->getRequest()->query->get('p')) {
  105.             $metaSetting 'noindex,follow';
  106.         }
  107.         $page $event->getPage();
  108.         if ($page instanceof Page) {
  109.             $metaInformation $page->getMetaInformation();
  110.             if (null !== $metaInformation) {
  111.                 $metaInformation->setRobots($metaSetting);
  112.             }
  113.         }
  114.     }
  115. }