Skip to content

Commit 9f46ab4

Browse files
ENGCOM-4258: [BugFix] Fix area for image placeholder in graphql response. #364
- Merge Pull Request magento/graphql-ce#364 from magento/graphql-ce:239_fix_resolving_of_image_placeholder_for_product_without_images - Merged commits: 1. 990f8ea 2. 8867a57 3. edcf04c 4. 9e0fb4b 5. e72d452 6. 83d9e75 7. 6c34401 8. 228ae14
2 parents 310648c + 228ae14 commit 9f46ab4

File tree

4 files changed

+162
-8
lines changed

4 files changed

+162
-8
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Product;
1111
use Magento\Catalog\Model\Product\ImageFactory;
12+
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image\Placeholder as PlaceholderProvider;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\Framework\GraphQl\Config\Element\Field;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -23,14 +24,21 @@ class Url implements ResolverInterface
2324
* @var ImageFactory
2425
*/
2526
private $productImageFactory;
27+
/**
28+
* @var PlaceholderProvider
29+
*/
30+
private $placeholderProvider;
2631

2732
/**
2833
* @param ImageFactory $productImageFactory
34+
* @param PlaceholderProvider $placeholderProvider
2935
*/
3036
public function __construct(
31-
ImageFactory $productImageFactory
37+
ImageFactory $productImageFactory,
38+
PlaceholderProvider $placeholderProvider
3239
) {
3340
$this->productImageFactory = $productImageFactory;
41+
$this->placeholderProvider = $placeholderProvider;
3442
}
3543

3644
/**
@@ -55,23 +63,27 @@ public function resolve(
5563
$product = $value['model'];
5664
$imagePath = $product->getData($value['image_type']);
5765

58-
$imageUrl = $this->getImageUrl($value['image_type'], $imagePath);
59-
return $imageUrl;
66+
return $this->getImageUrl($value['image_type'], $imagePath);
6067
}
6168

6269
/**
63-
* Get image url
70+
* Get image URL
6471
*
6572
* @param string $imageType
66-
* @param string|null $imagePath Null if image is not set
73+
* @param string|null $imagePath
6774
* @return string
75+
* @throws \Exception
6876
*/
6977
private function getImageUrl(string $imageType, ?string $imagePath): string
7078
{
7179
$image = $this->productImageFactory->create();
7280
$image->setDestinationSubdir($imageType)
7381
->setBaseFile($imagePath);
74-
$imageUrl = $image->getUrl();
75-
return $imageUrl;
82+
83+
if ($image->isBaseFilePlaceholder()) {
84+
return $this->placeholderProvider->getPlaceholder($imageType);
85+
}
86+
87+
return $image->getUrl();
7688
}
7789
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image;
9+
10+
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
11+
use Magento\Framework\View\Asset\Repository as AssetRepository;
12+
13+
/**
14+
* Image Placeholder provider
15+
*/
16+
class Placeholder
17+
{
18+
/**
19+
* @var PlaceholderFactory
20+
*/
21+
private $placeholderFactory;
22+
23+
/**
24+
* @var AssetRepository
25+
*/
26+
private $assetRepository;
27+
28+
/**
29+
* @param PlaceholderFactory $placeholderFactory
30+
* @param AssetRepository $assetRepository
31+
*/
32+
public function __construct(
33+
PlaceholderFactory $placeholderFactory,
34+
AssetRepository $assetRepository
35+
) {
36+
$this->placeholderFactory = $placeholderFactory;
37+
$this->assetRepository = $assetRepository;
38+
}
39+
40+
/**
41+
* Get placeholder
42+
*
43+
* @param string $imageType
44+
* @return string
45+
*/
46+
public function getPlaceholder(string $imageType): string
47+
{
48+
$imageAsset = $this->placeholderFactory->create(['type' => $imageType]);
49+
50+
// check if placeholder defined in config
51+
if ($imageAsset->getFilePath()) {
52+
return $imageAsset->getUrl();
53+
}
54+
55+
return $this->assetRepository->getUrl(
56+
"Magento_Catalog::images/product/placeholder/{$imageType}.jpg"
57+
);
58+
}
59+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Image\Placeholder;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Theme provider
16+
*/
17+
class Theme
18+
{
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private $scopeConfig;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @var ThemeProviderInterface
31+
*/
32+
private $themeProvider;
33+
34+
/**
35+
* @param ScopeConfigInterface $scopeConfig
36+
* @param StoreManagerInterface $storeManager
37+
* @param ThemeProviderInterface $themeProvider
38+
*/
39+
public function __construct(
40+
ScopeConfigInterface $scopeConfig,
41+
StoreManagerInterface $storeManager,
42+
ThemeProviderInterface $themeProvider
43+
) {
44+
$this->scopeConfig = $scopeConfig;
45+
$this->storeManager = $storeManager;
46+
$this->themeProvider = $themeProvider;
47+
}
48+
49+
/**
50+
* Get theme model
51+
*
52+
* @return array
53+
* @throws \Magento\Framework\Exception\NoSuchEntityException
54+
*/
55+
public function getThemeData(): array
56+
{
57+
$themeId = $this->scopeConfig->getValue(
58+
\Magento\Framework\View\DesignInterface::XML_PATH_THEME_ID,
59+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
60+
$this->storeManager->getStore()->getId()
61+
);
62+
63+
/** @var $theme \Magento\Framework\View\Design\ThemeInterface */
64+
$theme = $this->themeProvider->getThemeById($themeId);
65+
66+
$data = $theme->getData();
67+
$data['themeModel'] = $theme;
68+
69+
return $data;
70+
}
71+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductImageTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public function testProductWithBaseImage()
5151
*/
5252
public function testProductWithoutBaseImage()
5353
{
54-
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/239');
5554
$productSku = 'simple';
5655
$query = <<<QUERY
5756
{
@@ -61,12 +60,25 @@ public function testProductWithoutBaseImage()
6160
url
6261
label
6362
}
63+
small_image {
64+
url
65+
label
66+
}
6467
}
6568
}
6669
}
6770
QUERY;
6871
$response = $this->graphQlQuery($query);
6972
self::assertEquals('Simple Product', $response['products']['items'][0]['image']['label']);
73+
self::assertStringEndsWith(
74+
'images/product/placeholder/image.jpg',
75+
$response['products']['items'][0]['image']['url']
76+
);
77+
self::assertEquals('Simple Product', $response['products']['items'][0]['small_image']['label']);
78+
self::assertStringEndsWith(
79+
'images/product/placeholder/small_image.jpg',
80+
$response['products']['items'][0]['small_image']['url']
81+
);
7082
}
7183

7284
/**

0 commit comments

Comments
 (0)