vendor/store.shopware.com/coewishlistsw6/src/Storefront/Seo/WishlistSeoSubscriber.php line 74

Open in your IDE?
  1. <?php
  2. namespace CoeWishlistSw6\Storefront\Seo;
  3. use CoeWishlistSw6\Storefront\Seo\SeoUrlRoute\WishlistSeoUrlRoute;
  4. use Shopware\Core\Content\Seo\SeoUrl\SeoUrlEntity;
  5. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * Class WishlistSeoSubscriber
  20.  * @package CoeWishlistSw6\Storefront\Seo
  21.  * @author Jeffry Block <jeffry.block@codeenterprise.de>
  22.  */
  23. Class WishlistSeoSubscriber implements EventSubscriberInterface{
  24.     /**
  25.      * @var SeoUrlUpdater
  26.      */
  27.     private SeoUrlUpdater $seoUrlUpdater;
  28.     /**
  29.      * @var EntityRepository
  30.      */
  31.     private EntityRepository $seoUrlRepository;
  32.     /**
  33.      * WishlistSeoSubscriber constructor.
  34.      * @param SeoUrlUpdater $seoUrlUpdater
  35.      * @param EntityRepository $seoUrlRepository
  36.      */
  37.     public function __construct(SeoUrlUpdater $seoUrlUpdaterEntityRepository $seoUrlRepository) {
  38.         $this->seoUrlUpdater $seoUrlUpdater;
  39.         $this->seoUrlRepository $seoUrlRepository;
  40.     }
  41.     /**
  42.      * @return string[]
  43.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             'coe_wishlist.written' => 'onEntityWritten',
  49.             'coe_wishlist.deleted' => 'onEntityDeleted'
  50.         ];
  51.     }
  52.     /**
  53.      * @param EntityWrittenEvent $event
  54.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  55.      */
  56.     public function onEntityWritten(EntityWrittenEvent $event): void
  57.     {
  58.         $this->seoUrlUpdater->update(WishlistSeoUrlRoute::ROUTE_NAME$event->getIds());
  59.         $this->rewriteSeoUr($event->getContext());
  60.     }
  61.     /**
  62.      * @param EntityDeletedEvent $event
  63.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  64.      */
  65.     public function onEntityDeleted(EntityDeletedEvent $event): void
  66.     {
  67.         $this->seoUrlUpdater->update(WishlistSeoUrlRoute::ROUTE_NAME$event->getIds());
  68.         $this->rewriteSeoUr($event->getContext());
  69.     }
  70.     /**
  71.      * The SeoUpdater automatically saves the parameters in the pathInfo column as query (?a=1&b=2).
  72.      * But what we need is that the parameter is passed directly as a value as in the controller.
  73.      * Since there seems to be no configuration option for the SeoUpdater to do this, we simply do it ourselves with a simple search and replace.
  74.      * @param Context $context
  75.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  76.      */
  77.     private function rewriteSeoUr(Context $context){
  78.         $criteria = new Criteria();
  79.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND, [
  80.             new EqualsFilter('routeName'"frontend.wishlist.page"),
  81.             new ContainsFilter('pathInfo'"?"),
  82.         ]));
  83.         /** @var EntitySearchResult $result */
  84.         $result $this->seoUrlRepository->search($criteria$context);
  85.         $updates = [];
  86.         /** @var SeoUrlEntity $seoUrlEntity */
  87.         foreach ($result->getEntities()->getIterator() as $seoUrlEntity) {
  88.             $updates[] = [
  89.                 "id" => $seoUrlEntity->getId(),
  90.                 "pathInfo" => str_replace("?wishlistId=","/"$seoUrlEntity->getPathInfo())
  91.             ];
  92.         }
  93.         if (!empty($updates)) {
  94.             $this->seoUrlRepository->update($updates$context);
  95.         }
  96.     }
  97. }