vendor/shopware/core/Framework/Adapter/Twig/Extension/MediaExtension.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Adapter\Twig\Extension;
  3. use Shopware\Core\Content\Media\MediaCollection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFunction;
  9. class MediaExtension extends AbstractExtension
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface
  13.      */
  14.     private $mediaRepository;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(EntityRepositoryInterface $mediaRepository)
  19.     {
  20.         $this->mediaRepository $mediaRepository;
  21.     }
  22.     public function getFunctions(): array
  23.     {
  24.         return [
  25.             new TwigFunction('searchMedia', [$this'searchMedia']),
  26.         ];
  27.     }
  28.     public function searchMedia(array $idsContext $context): MediaCollection
  29.     {
  30.         if (empty($ids)) {
  31.             return new MediaCollection();
  32.         }
  33.         $criteria = new Criteria($ids);
  34.         /** @var MediaCollection $media */
  35.         $media $this->mediaRepository
  36.             ->search($criteria$context)
  37.             ->getEntities();
  38.         return $media;
  39.     }
  40. }