Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Media.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
21 
25  private $swatchHelper;
26 
32  public function __construct(
33  Context $context,
34  \Magento\Catalog\Model\ProductFactory $productModelFactory,
35  \Magento\Swatches\Helper\Data $swatchHelper
36  ) {
37  $this->productModelFactory = $productModelFactory;
38  $this->swatchHelper = $swatchHelper;
39 
40  parent::__construct($context);
41  }
42 
48  public function execute()
49  {
50  $productMedia = [];
51  if ($productId = (int)$this->getRequest()->getParam('product_id')) {
52  $productMedia = $this->swatchHelper->getProductMediaGallery(
53  $this->productModelFactory->create()->load($productId)
54  );
55  }
56 
58  $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
59  $resultJson->setData($productMedia);
60  return $resultJson;
61  }
62 }
__construct(Context $context, \Magento\Catalog\Model\ProductFactory $productModelFactory, \Magento\Swatches\Helper\Data $swatchHelper)
Definition: Media.php:32