vendor/store.shopware.com/ndsrecipemanager/src/Subscriber/RecipeSubscriber.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nds\RecipeManager\Subscriber;
  3. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. class RecipeSubscriber implements EventSubscriberInterface
  9. {
  10.     private $recipeRepository;
  11.     public function __construct(
  12.         EntityRepositoryInterface $recipeRepository
  13.     ) {
  14.         $this->recipeRepository $recipeRepository;
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  20.         ];
  21.     }
  22.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  23.     {
  24.         $context $event->getSalesChannelContext()->getContext();
  25.         $productId $event->getPage()->getProduct()->getId();
  26.         $recipeExtension['recipeExtension'] = [];
  27.         $criteria = new Criteria();
  28.         $criteria->addAssociation('ingredients');
  29.         $criteria->addFilter(new EqualsFilter('ingredients.productId'$productId));
  30.         $recipes $this->recipeRepository->search($criteria$context);
  31.         if ($recipes->getElements()) {
  32.             $recipeExtension['recipeExtension'] = array_values($recipes->getElements());
  33.             $event->getPage()->setExtensions($recipeExtension);
  34.         }
  35.     }
  36. }