custom/plugins/OttBrandCategorySeoUrlFix/src/Subscriber/EntityIndexerSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ott\BrandCategorySeoUrlFix\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Defaults;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  6. use Shopware\Core\Framework\Event\ProgressFinishedEvent;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
  10. class EntityIndexerSubscriber implements EventSubscriberInterface
  11. {
  12.     private const MOEBELFIRST_SALES_CHANNEL_ID 'c1aedaadc2d54272bc6b5450488a895e';
  13.     private const BRAND_CATEGORY_ID '981e2967dbf141f1a574231eeb721ac9';
  14.     private Connection $connection;
  15.     public function __construct(Connection $connection)
  16.     {
  17.         $this->connection $connection;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ProgressFinishedEvent::class => 'onProgressFinishedEvent',
  23.             CategoryIndexerEvent::class  => 'onCategoryIndexerEvent',
  24.         ];
  25.     }
  26.     public function onProgressFinishedEvent(ProgressFinishedEvent $event): void
  27.     {
  28.         if ('category.indexer' !== $event->getMessage()) {
  29.             return;
  30.         }
  31.         $this->fixSeoUrl();
  32.     }
  33.     public function onCategoryIndexerEvent(CategoryIndexerEvent $event): void
  34.     {
  35.         if (\in_array(self::BRAND_CATEGORY_ID$event->getIds())) {
  36.             $this->fixSeoUrl();
  37.         }
  38.     }
  39.     private function fixSeoUrl(): void
  40.     {
  41.         $statement = <<<'SQL'
  42.             DELETE FROM seo_url WHERE seo_path_info = 'marken/'
  43.             SQL;
  44.         $this->connection->prepare($statement)->execute();
  45.         $statement = <<<'SQL'
  46.             INSERT INTO seo_url
  47.                 (
  48.                  id,
  49.                  language_id,
  50.                  sales_channel_id,
  51.                  foreign_key,
  52.                  route_name,
  53.                  path_info,
  54.                  seo_path_info,
  55.                  is_canonical,
  56.                  is_modified,
  57.                  is_deleted,
  58.                  custom_fields,
  59.                  created_at,
  60.                  updated_at
  61.                  )
  62.             VALUES
  63.                 (
  64.                  UNHEX(:id),
  65.                  UNHEX(:languageId),
  66.                  UNHEX(:salesChannelId),
  67.                  UNHEX(:foreignKey),
  68.                  'frontend.cbax.manufacturer.index',
  69.                  '/cbax/manufacturer/index',
  70.                  'marken/',
  71.                  1,
  72.                  0,
  73.                  0,
  74.                  NULL,
  75.                  NOW(),
  76.                  NOW()
  77.                  )
  78.             SQL;
  79.         $preparedStatement $this->connection->prepare($statement);
  80.         $preparedStatement->bindValue('id'Uuid::randomHex(), \PDO::PARAM_STR);
  81.         $preparedStatement->bindValue('languageId'DEFAULTS::LANGUAGE_SYSTEM\PDO::PARAM_STR);
  82.         $preparedStatement->bindValue('salesChannelId'self::MOEBELFIRST_SALES_CHANNEL_ID\PDO::PARAM_STR);
  83.         $preparedStatement->bindValue('foreignKey'self::BRAND_CATEGORY_ID\PDO::PARAM_STR);
  84.         $preparedStatement->execute();
  85.     }
  86. }