custom/plugins/MageWorxSeoCrosslinks/src/Subscriber/CategorySubscriber.php line 54

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MageWorx\SeoCrosslinks\Subscriber;
  3. use MageWorx\SeoCrosslinks\Core\Content\Crosslink\Service\SalesChannelContextProvider;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Content\Category\CategoryEvents;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use MageWorx\SeoCrosslinks\Core\Content\Crosslink\Service\CrosslinkReplacer;
  16. class CategorySubscriber implements EventSubscriberInterface
  17. {
  18.     protected RequestStack $requestStack;
  19.     protected SystemConfigService $systemConfig;
  20.     protected EntityRepositoryInterface $crosslinkRepository;
  21.     protected CrosslinkReplacer $replacer;
  22.     protected SalesChannelContextProvider $salesChannelContextProvider;
  23.     protected $result;
  24.     public function __construct(
  25.         EntityRepositoryInterface $crosslinkRepository,
  26.         SystemConfigService $systemConfigService,
  27.         RequestStack $requestStack,
  28.         CrosslinkReplacer $replacer,
  29.         SalesChannelContextProvider $salesChannelContextProvider
  30.     ) {
  31.         $this->crosslinkRepository $crosslinkRepository;
  32.         $this->requestStack $requestStack;
  33.         $this->systemConfig $systemConfigService;
  34.         $this->replacer $replacer;
  35.         $this->salesChannelContextProvider $salesChannelContextProvider;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             CategoryEvents::CATEGORY_LOADED_EVENT => 'onCategoriesLoaded'
  41.         ];
  42.     }
  43.     public function onCategoriesLoaded(EntityLoadedEvent $event)
  44.     {
  45.         if ($this->isAllow()) {
  46.             $id $this->requestStack->getMainRequest()->attributes->get('navigationId');
  47.             $maxReplaceCount $this->systemConfig->getInt('MageWorxSeoCrosslinks.config.categoryMaxReplacementCount');
  48.             if ($maxReplaceCount) {
  49.                 $category current($event->getEntities());
  50.                 if ($category && $category->getId() === $id) {
  51.                     $translated $category->getTranslated();
  52.                     if ($translated) {
  53.                         if ($this->result === null && $translated['description']) {
  54.                             $description $translated['description'];
  55.                             if ($description) {
  56.                                 $this->result $this->getModifiedContent(
  57.                                     $description,
  58.                                     $category->getId(),
  59.                                     $event->getContext(),
  60.                                     $maxReplaceCount
  61.                                 );
  62.                             }
  63.                         }
  64.                         if ($this->result) {
  65.                             $translated['description'] = $this->result;
  66.                             $category->setTranslated($translated);
  67.                             $category->setDescription($translated['description']);
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     protected function isAllow(): bool
  75.     {
  76.         if ($this->systemConfig->getBool('MageWorxSeoCrosslinks.config.enable')) {
  77.             $request $this->requestStack->getMainRequest();
  78.             if ($request) {
  79.                 $route $request->attributes->get('_route');
  80.                 $categoryId $request->attributes->get('navigationId');
  81.                 if ($route === 'frontend.navigation.page' && $categoryId) {
  82.                     return true;
  83.                 }
  84.             }
  85.         }
  86.         return false;
  87.     }
  88.     /**
  89.      * @param string $content
  90.      * @param string $entityId
  91.      * @param Context $context
  92.      * @param int $maxCount
  93.      * @return false|string
  94.      */
  95.     protected function getModifiedContent(
  96.         string $content,
  97.         string $entityId,
  98.         Context $context,
  99.         int $maxCount
  100.     ) {
  101.         $criteria = new Criteria();
  102.         $criteria->addFilter(
  103.             new EqualsFilter('isActive'true),
  104.             new EqualsFilter('inCategory'true),
  105.             new EqualsFilter('languageId'$context->getLanguageId()),
  106.             new EqualsFilter(
  107.                 'salesChannels.salesChannelId',
  108.                 $this->salesChannelContextProvider->getSalesChannelContext($context)->getSalesChannelId()
  109.             ),
  110.             new NotFilter('AND', [
  111.                 new EqualsFilter('internalLink'$entityId)
  112.             ]),
  113.         );
  114.         $criteria->addSorting(new FieldSorting('priority''DESC'));
  115.         $crosslinkCollection $this->crosslinkRepository->search($criteria$context)->getEntities();
  116.         return $this->replacer->replace($context$crosslinkCollection$content$maxCount);
  117.     }
  118. }