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

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 Shopware\Core\Framework\Log\Package;
  8. use Twig\Extension\AbstractExtension;
  9. use Twig\TwigFunction;
  10. #[Package('core')]
  11. class MediaExtension extends AbstractExtension
  12. {
  13.     /**
  14.      * @var EntityRepositoryInterface
  15.      */
  16.     private $mediaRepository;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(EntityRepositoryInterface $mediaRepository)
  21.     {
  22.         $this->mediaRepository $mediaRepository;
  23.     }
  24.     public function getFunctions(): array
  25.     {
  26.         return [
  27.             new TwigFunction('searchMedia', [$this'searchMedia']),
  28.         ];
  29.     }
  30.     public function searchMedia(array $idsContext $context): MediaCollection
  31.     {
  32.         if (empty($ids)) {
  33.             return new MediaCollection();
  34.         }
  35.         $criteria = new Criteria($ids);
  36.         /** @var MediaCollection $media */
  37.         $media $this->mediaRepository
  38.             ->search($criteria$context)
  39.             ->getEntities();
  40.         return $media;
  41.     }
  42. }