Media.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Swatches\Controller\Ajax;
  8. use Magento\Framework\App\Action\Context;
  9. use Magento\Framework\Controller\ResultFactory;
  10. /**
  11. * Class Media
  12. */
  13. class Media extends \Magento\Framework\App\Action\Action implements \Magento\Framework\App\Action\HttpGetActionInterface
  14. {
  15. /**
  16. * @var \Magento\Catalog\Model\Product Factory
  17. */
  18. protected $productModelFactory;
  19. /**
  20. * @var \Magento\Swatches\Helper\Data
  21. */
  22. private $swatchHelper;
  23. /**
  24. * @var \Magento\PageCache\Model\Config
  25. */
  26. protected $config;
  27. /**
  28. * @param Context $context
  29. * @param \Magento\Catalog\Model\ProductFactory $productModelFactory
  30. * @param \Magento\Swatches\Helper\Data $swatchHelper
  31. * @param \Magento\PageCache\Model\Config $config
  32. */
  33. public function __construct(
  34. Context $context,
  35. \Magento\Catalog\Model\ProductFactory $productModelFactory,
  36. \Magento\Swatches\Helper\Data $swatchHelper,
  37. \Magento\PageCache\Model\Config $config
  38. ) {
  39. $this->productModelFactory = $productModelFactory;
  40. $this->swatchHelper = $swatchHelper;
  41. $this->config = $config;
  42. parent::__construct($context);
  43. }
  44. /**
  45. * Get product media for specified configurable product variation
  46. *
  47. * @return string
  48. * @throws \Magento\Framework\Exception\LocalizedException
  49. */
  50. public function execute()
  51. {
  52. $productMedia = [];
  53. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  54. $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
  55. /** @var \Magento\Framework\App\ResponseInterface $response */
  56. $response = $this->getResponse();
  57. if ($productId = (int)$this->getRequest()->getParam('product_id')) {
  58. $product = $this->productModelFactory->create()->load($productId);
  59. $productMedia = $this->swatchHelper->getProductMediaGallery(
  60. $product
  61. );
  62. $resultJson->setHeader('X-Magento-Tags', implode(',', $product->getIdentities()));
  63. $response->setPublicHeaders($this->config->getTtl());
  64. }
  65. $resultJson->setData($productMedia);
  66. return $resultJson;
  67. }
  68. }