vendor/shopware/core/Content/Category/Subscriber/CategorySubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Category\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\CategoryEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CategorySubscriber implements EventSubscriberInterface
  11. {
  12.     private SystemConfigService $systemConfigService;
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(
  17.         SystemConfigService $systemConfigService
  18.     ) {
  19.         $this->systemConfigService $systemConfigService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CategoryEvents::CATEGORY_LOADED_EVENT => 'entityLoaded',
  25.             'sales_channel.' CategoryEvents::CATEGORY_LOADED_EVENT => 'entityLoaded',
  26.         ];
  27.     }
  28.     public function entityLoaded(EntityLoadedEvent $event): void
  29.     {
  30.         $salesChannelId $event instanceof SalesChannelEntityLoadedEvent $event->getSalesChannelContext()->getSalesChannelId() : null;
  31.         /** @var CategoryEntity $category */
  32.         foreach ($event->getEntities() as $category) {
  33.             $categoryCmsPageId $category->getCmsPageId();
  34.             // continue if cms page is given and was not set in the subscriber
  35.             if ($categoryCmsPageId !== null && !$category->getCmsPageIdSwitched()) {
  36.                 continue;
  37.             }
  38.             // continue if cms page is given and not the overall default
  39.             if ($categoryCmsPageId !== null && $categoryCmsPageId !== $this->systemConfigService->get(CategoryDefinition::CONFIG_KEY_DEFAULT_CMS_PAGE_CATEGORY)) {
  40.                 continue;
  41.             }
  42.             $userDefault $this->systemConfigService->get(CategoryDefinition::CONFIG_KEY_DEFAULT_CMS_PAGE_CATEGORY$salesChannelId);
  43.             // cms page is not given in system config
  44.             if ($userDefault === null) {
  45.                 continue;
  46.             }
  47.             /** @var string $userDefault */
  48.             $category->setCmsPageId($userDefault);
  49.             // mark cms page as set in the subscriber
  50.             $category->setCmsPageIdSwitched(true);
  51.         }
  52.     }
  53. }