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

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