Skip to content

Commit bb4f601

Browse files
author
Valeriy Nayda
committed
GraphQL-174: Absolute image paths for Products
1 parent 7867e91 commit bb4f601

File tree

2 files changed

+22
-101
lines changed

2 files changed

+22
-101
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/Image.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductImage.php

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,34 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

1010
use Magento\Catalog\Model\Product;
11-
use Magento\Catalog\Helper\ImageFactory as CatalogImageHelperFactory;
11+
use Magento\Catalog\Model\Product\ImageFactory;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
13-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1414
use Magento\Framework\GraphQl\Query\ResolverInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16-
use Magento\Store\Model\StoreManagerInterface;
1716

1817
/**
19-
* Return product image paths by image type.
18+
* Returns product's image data
2019
*/
2120
class ProductImage implements ResolverInterface
2221
{
2322
/**
24-
* @var CatalogImageHelperFactory
25-
*/
26-
private $catalogImageHelperFactory;
27-
28-
/**
29-
* @var StoreManagerInterface
23+
* Product image factory
24+
*
25+
* @var ImageFactory
3026
*/
31-
private $storeManager;
27+
private $productImageFactory;
3228

3329
/**
34-
* @param CatalogImageHelperFactory $catalogImageHelperFactory
35-
* @param StoreManagerInterface $storeManager
30+
* @param ImageFactory $productImageFactory
3631
*/
3732
public function __construct(
38-
CatalogImageHelperFactory $catalogImageHelperFactory,
39-
StoreManagerInterface $storeManager
33+
ImageFactory $productImageFactory
4034
) {
41-
$this->catalogImageHelperFactory = $catalogImageHelperFactory;
42-
$this->storeManager = $storeManager;
35+
$this->productImageFactory = $productImageFactory;
4336
}
4437

4538
/**
46-
* Get product's image by type.
47-
*
4839
* @inheritdoc
4940
*/
5041
public function resolve(
@@ -53,30 +44,26 @@ public function resolve(
5344
ResolveInfo $info,
5445
array $value = null,
5546
array $args = null
56-
) {
47+
): array {
5748
if (!isset($value['model'])) {
58-
throw new GraphQlInputException(__('"model" value should be specified'));
49+
throw new LocalizedException(__('"model" value should be specified'));
5950
}
6051
/** @var Product $product */
6152
$product = $value['model'];
6253
$imageType = $field->getName();
6354

64-
/** @var \Magento\Catalog\Helper\Image $catalogImageHelper */
65-
$catalogImageHelper = $this->catalogImageHelperFactory->create();
55+
$imagePath = $product->getData($imageType);
56+
$imageLabel = $product->getData($imageType . '_' . 'label') ?? $product->getName();
6657

67-
/** @var \Magento\Catalog\Helper\Image $image */
68-
$image = $catalogImageHelper->init(
69-
$product,
70-
'product_' . $imageType,
71-
['type' => $imageType]
72-
);
58+
$image = $this->productImageFactory->create();
59+
$image->setDestinationSubdir($imageType)
60+
->setBaseFile($imagePath);
61+
$imageUrl = $image->getUrl();
7362

74-
$imageData = [
75-
'url' => $image->getUrl(),
76-
'path' => $product->getData($imageType),
77-
'label' => $image->getLabel()
63+
return [
64+
'url' => $imageUrl,
65+
'path' => $imagePath,
66+
'label' => $imageLabel,
7867
];
79-
80-
return $imageData;
8168
}
8269
}

0 commit comments

Comments
 (0)