custom/plugins/dupp_sync4_api/src/Subscriber/CategoryLoadedSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dupp\Sync4Api\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  5. use Shopware\Core\Content\Category\CategoryEvents;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CategoryLoadedSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var AbstractCategoryRoute
  15.      */
  16.     private $cmsPageRoute;
  17.     public function __construct(
  18.         AbstractCategoryRoute $cmsPageRoute
  19.     )
  20.     {
  21.         $this->cmsPageRoute $cmsPageRoute;
  22.     }
  23.     /**
  24.      * @var string
  25.      */
  26.     private $pluginName 'Sync4ApiExport';
  27.     /**
  28.      * @var string
  29.      */
  30.     private $customFieldName 'sync4_api_visual_order';
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded'
  35.         ];
  36.     }
  37.     public function onNavigationPageLoaded(NavigationPageLoadedEvent $event)
  38.     {
  39.         try {
  40.             $this->handleCustomFields($event);
  41.         } catch (\Exception $exception) {
  42.             //TODO: add logging
  43.         }
  44.     }
  45.     private function handleCustomFields(NavigationPageLoadedEvent $event)
  46.     {
  47.         $config = [];
  48.         $navigationId $event->getRequest()->get(
  49.             'navigationId',
  50.             $event->getSalesChannelContext()->getSalesChannel()->getNavigationCategoryId()
  51.         );
  52.         $category $this->cmsPageRoute
  53.             ->load($navigationId$event->getRequest(), $event->getSalesChannelContext())
  54.             ->getCategory();
  55.         if (!$category) {
  56.             return;
  57.         }
  58.         $customFields $category->get('customFields');
  59.         if (!$customFields) {
  60.             return;
  61.         }
  62.         foreach($customFields as $name => $value) {
  63.             if ($name !== $this->customFieldName || empty($value)) {
  64.                 continue;
  65.             }
  66.             $config['visualOrder'] = json_decode($value);
  67.         }
  68.         $event->getContext()->addExtension($this->pluginName, new ArrayEntity($config));
  69.     }
  70. }