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

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