custom/plugins/DreiscSeoPro/src/Subscriber/ProductEvents/ProductLoadedSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Subscriber\ProductEvents;
  3. use DreiscSeoPro\Core\Seo\LiveTemplate\LiveTemplateConverter;
  4. use PHPUnit\Exception;
  5. use Shopware\Core\Content\Category\CategoryEntity;
  6. use Shopware\Core\Content\Category\CategoryEvents;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductLoadedSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var LiveTemplateConverter
  16.      */
  17.     private $liveTemplateConverter;
  18.     /**
  19.      * @param LiveTemplateConverter $liveTemplateTranslator
  20.      */
  21.     public function __construct(LiveTemplateConverter $liveTemplateTranslator)
  22.     {
  23.         $this->liveTemplateConverter $liveTemplateTranslator;
  24.     }
  25.     /**
  26.      * @return array|void
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
  32.         ];
  33.     }
  34.     /**
  35.      * @param EntityLoadedEvent $entityLoadedEvent
  36.      */
  37.     public function onProductLoaded(EntityLoadedEvent $entityLoadedEvent): void
  38.     {
  39.         /** @var ProductEntity $productEntity */
  40.         $productEntity current($entityLoadedEvent->getEntities());
  41.         /** Abort, if empty */
  42.         if (false === $productEntity) {
  43.             return;
  44.         }
  45.         /** At this point the seo live template will be converted */
  46.         $this->liveTemplateConverter->translateProductEntity($productEntity$entityLoadedEvent->getContext());
  47.     }
  48. }