vendor/gourmops/product-ingredients/src/Subscriber/ProductPageSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * GourmopsProduct
  4.  * Copyright (c) Simone Gottwald GOURMOPS
  5.  */
  6. namespace GourmopsProductIngredients\Subscriber;
  7. use GourmopsProductIngredients\GourmopsProductIngredients;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Storefront\Page\Product\ProductPage;
  10. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  11. /**
  12.  * Class ProductPageSubscriber
  13.  */
  14. class ProductPageSubscriber extends AbstractSubscriber
  15. {
  16.     /**
  17.      * @return string[]
  18.      */
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  23.         ];
  24.     }
  25.     /**
  26.      * @param ProductPageLoadedEvent $event
  27.      *
  28.      * @return void
  29.      */
  30.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  31.     {
  32.         /** @var array $config */
  33.         $config $this->getConfig($event->getSalesChannelContext());
  34.         /** @var ProductPage $page */
  35.         $page $event->getPage();
  36.         /** @var array|null $customFields */
  37.         $customFields $this->getCustomFields($page->getProduct(), $event->getContext());
  38.         if (empty($config) ||
  39.             empty($config['activated']) ||
  40.             empty($customFields)
  41.         ) {
  42.             return;
  43.         }
  44.         /** @var ArrayStruct $arrayStruct */
  45.         $arrayStruct = new ArrayStruct();
  46.         $arrayStruct->set(
  47.             'config',
  48.             [
  49.                 'custom_fields' => $customFields,
  50.             ]
  51.         );
  52.         $page->addExtension(GourmopsProductIngredients::FIELD_PLACEHOLDER$arrayStruct);
  53.     }
  54. }