<?php
declare(strict_types=1);
namespace EconsorSetup\Subscriber;
use Exception;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Page;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
class EconsorSetupRobotMetaSubscriber implements EventSubscriberInterface
{
private EntityRepository $productRepository;
private string $devInstalled;
public function __construct(EntityRepository $productRepository, string $devInstalled)
{
$this->productRepository = $productRepository;
$this->devInstalled = $devInstalled;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onPageLoaded',
];
}
public function onPageLoaded(PageLoadedEvent $event): void
{
try {
// check for Customfields, if EconsorDev Plugin is NOT active via .xml Parameter.
$request = $event->getRequest();
if (!$request->isXmlHttpRequest() && "false" === $this->devInstalled) {
$route = $request->attributes->get("_route");
assert(is_string($route));
switch ($route) {
case 'frontend.home.page':
$customFields = $this->getHomeCustomFields($event);
break;
case 'frontend.navigation.page':
$customFields = $this->getNavigationCustomFields($event);
break;
case 'frontend.detail.page':
$customFields = $this->getDetailCustomFields($event, $request);
break;
default:
$customFields = null;
}
$this->setRobots($event, $customFields);
}
} catch (Exception $e) {
/** TODO: log error */
}
}
private function getHomeCustomFields(PageLoadedEvent $event): ?array
{
return $event->getSalesChannelContext()->getSalesChannel()->getCustomFields();
}
private function getNavigationCustomFields(PageLoadedEvent $event): ?array
{
if (!($page = $event->getPage()) instanceof Page) {
return null;
}
if (null === $header = $page->getHeader()) {
return null;
}
if (null === $tree = $header->getNavigation()) {
return null;
}
if (null === $active = $tree->getActive()) {
return null;
}
return $active->getCustomFields();
}
private function getDetailCustomFields(PageLoadedEvent $event, Request $request): ?array
{
$productId = $request->query->get("productId");
assert(is_string($productId));
try {
$result = $this->productRepository->search(
(new Criteria([$productId]))->addAssociation('customFields'),
$event->getSalesChannelContext()->getContext()
);
if ($result->getTotal()) {
/** @var ProductEntity $product */
$product = $result->first();
return $product->getCustomFields();
}
} catch (Exception $e) {
/** TODO: log error */
}
return null;
}
private function setRobots(PageLoadedEvent $event, ?array $customFields): void
{
$metaSetting = 'index,follow';
if (isset($customFields)) {
if (array_key_exists('custom_meta_robots', $customFields)) {
if (true === $customFields['custom_meta_robots']) {
$metaSetting = 'noindex,nofollow';
}
}
}
if ($event->getRequest()->query->get('p')) {
$metaSetting = 'noindex,follow';
}
$page = $event->getPage();
if ($page instanceof Page) {
$metaInformation = $page->getMetaInformation();
if (null !== $metaInformation) {
$metaInformation->setRobots($metaSetting);
}
}
}
}