diff --git a/app/code/Magento/Catalog/Block/Product/View/Gallery.php b/app/code/Magento/Catalog/Block/Product/View/Gallery.php index e7817243ac2a7..484dbb2cd646e 100644 --- a/app/code/Magento/Catalog/Block/Product/View/Gallery.php +++ b/app/code/Magento/Catalog/Block/Product/View/Gallery.php @@ -12,6 +12,7 @@ namespace Magento\Catalog\Block\Product\View; use Magento\Framework\Data\Collection; +use Magento\Framework\Json\EncoderInterface; use Magento\Catalog\Helper\Image; class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView @@ -21,6 +22,27 @@ class Gallery extends \Magento\Catalog\Block\Product\View\AbstractView */ protected $configView; + /** + * @var \Magento\Framework\Json\EncoderInterface + */ + protected $jsonEncoder; + + /** + * @param \Magento\Catalog\Block\Product\Context $context + * @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils + * @param EncoderInterface $jsonEncoder + * @param array $data + */ + public function __construct( + \Magento\Catalog\Block\Product\Context $context, + \Magento\Framework\Stdlib\ArrayUtils $arrayUtils, + EncoderInterface $jsonEncoder, + array $data = [] + ) { + $this->jsonEncoder = $jsonEncoder; + parent::__construct($context, $arrayUtils, $data); + } + /** * Retrieve collection of gallery images * @@ -42,12 +64,14 @@ public function getGalleryImages() $image->setData( 'medium_image_url', $this->_imageHelper->init($product, 'product_page_image_medium') + ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false) ->setImageFile($image->getFile()) ->getUrl() ); $image->setData( 'large_image_url', $this->_imageHelper->init($product, 'product_page_image_large') + ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false) ->setImageFile($image->getFile()) ->getUrl() ); @@ -57,6 +81,26 @@ public function getGalleryImages() return $images; } + /** + * Return magnifier options + * + * @return string + */ + public function getMagnifier() + { + return $this->jsonEncoder->encode($this->getVar('magnifier')); + } + + /** + * Return breakpoints options + * + * @return string + */ + public function getBreakpoints() + { + return $this->jsonEncoder->encode($this->getVar('breakpoints')); + } + /** * Retrieve product images in JSON format * @@ -69,12 +113,22 @@ public function getGalleryImagesJson() $imagesItems[] = [ 'thumb' => $image->getData('small_image_url'), 'img' => $image->getData('medium_image_url'), - 'original' => $image->getData('large_image_url'), + 'full' => $image->getData('large_image_url'), 'caption' => $image->getLabel(), 'position' => $image->getPosition(), 'isMain' => $this->isMainImage($image), ]; } + if (empty($imagesItems)) { + $imagesItems[] = [ + 'thumb' => $this->_imageHelper->getDefaultPlaceholderUrl('thumbnail'), + 'img' => $this->_imageHelper->getDefaultPlaceholderUrl('image'), + 'full' => $this->_imageHelper->getDefaultPlaceholderUrl('image'), + 'caption' => '', + 'position' => '0', + 'isMain' => true, + ]; + } return json_encode($imagesItems); } diff --git a/app/code/Magento/Catalog/Helper/Image.php b/app/code/Magento/Catalog/Helper/Image.php index c4ceb6bea1f0d..a847e8c42454a 100644 --- a/app/code/Magento/Catalog/Helper/Image.php +++ b/app/code/Magento/Catalog/Helper/Image.php @@ -436,11 +436,13 @@ public function placeholder($fileName) */ public function getPlaceholder($placeholder = null) { - if (!$this->_placeholder) { - $placeholder = $placeholder ? : $this->_getModel()->getDestinationSubdir(); - $this->_placeholder = 'Magento_Catalog::images/product/placeholder/' . $placeholder . '.jpg'; + if ($placeholder) { + $placeholderFullPath = 'Magento_Catalog::images/product/placeholder/' . $placeholder . '.jpg'; + } else { + $placeholderFullPath = $this->_placeholder + ?: 'Magento_Catalog::images/product/placeholder/' . $this->_getModel()->getDestinationSubdir() . '.jpg'; } - return $this->_placeholder; + return $placeholderFullPath; } /** @@ -816,7 +818,7 @@ public function getWidth() */ public function getHeight() { - return $this->getAttribute('height') ? : $this->getAttribute('width'); + return $this->getAttribute('height') ?: $this->getAttribute('width'); } /** diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index a67d5c4dabacc..99709b513ca2c 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -19,6 +19,7 @@ * Catalog product model * * @method Product setHasError(bool $value) + * @method \Magento\Catalog\Model\ResourceModel\Product getResource() * @method null|bool getHasError() * @method Product setAssociatedProductIds(array $productIds) * @method array getAssociatedProductIds() diff --git a/app/code/Magento/Catalog/Model/Product/Image.php b/app/code/Magento/Catalog/Model/Product/Image.php index 5ad174c1ae3fd..3d750be576b98 100644 --- a/app/code/Magento/Catalog/Model/Product/Image.php +++ b/app/code/Magento/Catalog/Model/Product/Image.php @@ -19,6 +19,9 @@ * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @method string getFile() + * @method string getLabel() + * @method string getPosition() */ class Image extends \Magento\Framework\Model\AbstractModel { diff --git a/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php b/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php index 564e08a1cbc61..eec42410e2e81 100644 --- a/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Block/Product/View/GalleryTest.php @@ -32,6 +32,11 @@ class GalleryTest extends \PHPUnit_Framework_TestCase */ protected $registry; + /** + * @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $jsonEncoderMock; + protected function setUp() { $this->mockContext(); @@ -40,9 +45,14 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); + $this->jsonEncoderMock = $this->getMockBuilder('Magento\Framework\Json\EncoderInterface') + ->disableOriginalConstructor() + ->getMock(); + $this->model = new \Magento\Catalog\Block\Product\View\Gallery( $this->context, - $this->arrayUtils + $this->arrayUtils, + $this->jsonEncoderMock ); } @@ -103,7 +113,8 @@ public function testGetGalleryImages() [$productMock, 'product_page_image_small', [], $this->imageHelper], [$productMock, 'product_page_image_medium', [], $this->imageHelper], [$productMock, 'product_page_image_large', [], $this->imageHelper], - ]); + ]) + ->willReturnSelf(); $this->imageHelper->expects($this->exactly(3)) ->method('setImageFile') ->with('test_file') @@ -118,6 +129,19 @@ public function testGetGalleryImages() ->method('getUrl') ->willReturn('product_page_image_large_url'); + $this->imageHelper->expects($this->exactly(2)) + ->method('constrainOnly') + ->with(true) + ->willReturnSelf(); + $this->imageHelper->expects($this->exactly(2)) + ->method('keepAspectRatio') + ->with(true) + ->willReturnSelf(); + $this->imageHelper->expects($this->exactly(2)) + ->method('keepFrame') + ->with(false) + ->willReturnSelf(); + $images = $this->model->getGalleryImages(); $this->assertInstanceOf('Magento\Framework\Data\Collection', $images); } diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/view/gallery.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/view/gallery.phtml index e2ec81f81a8a1..d344cef700db1 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/view/gallery.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/view/gallery.phtml @@ -12,49 +12,50 @@ * @var $block \Magento\Catalog\Block\Product\View\Gallery */ ?> -
+