Skip to content

Commit c366d47

Browse files
author
Valeriy Nayda
committed
GraphQL-174: Absolute image paths for Products
1 parent 3d3b45f commit c366d47

File tree

2 files changed

+20
-11
lines changed
  • app/code/Magento/CatalogGraphQl/Model/Resolver/Product/ProductImage

2 files changed

+20
-11
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,33 @@ public function resolve(
6262

6363
/** @var Product $product */
6464
$product = $value['model'];
65+
$imageType = $value['image_type'];
66+
$imagePath = $product->getData($imageType);
67+
$productId = (int)$product->getEntityId();
68+
69+
// null if image is not set
70+
if (null === $imagePath) {
71+
return $this->getAttributeValue($productId, 'name');
72+
}
73+
74+
$imageLabel = $this->getAttributeValue($productId, $imageType . '_label');
75+
if (null === $imageLabel) {
76+
$imageLabel = $this->getAttributeValue($productId, 'name');
77+
}
6578

66-
$imageLabel = $this->getImageLabel((int)$product->getEntityId(), $value['image_type']);
6779
return $imageLabel;
6880
}
6981

7082
/**
7183
* @param int $productId
72-
* @param string $imageType
73-
* @return string
84+
* @param string $attributeCode
85+
* @return null|string Null if attribute value is not exists
7486
*/
75-
private function getImageLabel(int $productId, string $imageType): string
87+
private function getAttributeValue(int $productId, string $attributeCode): ?string
7688
{
7789
$storeId = $this->storeManager->getStore()->getId();
7890

79-
$imageLabel = $this->productResource->getAttributeRawValue($productId, $imageType . '_label', $storeId);
80-
if (empty($imageLabel)) {
81-
$imageLabel = $this->productResource->getAttributeRawValue($productId, 'name', $storeId);
82-
}
83-
return $imageLabel;
91+
$value = $this->productResource->getAttributeRawValue($productId, $attributeCode, $storeId);
92+
return is_array($value) && empty($value) ? null : $value;
8493
}
8594
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ public function resolve(
6161

6262
/**
6363
* @param string $imageType
64-
* @param string $imagePath
64+
* @param string|null $imagePath Null if image is not set
6565
* @return string
6666
*/
67-
private function getImageUrl(string $imageType, string $imagePath): string
67+
private function getImageUrl(string $imageType, ?string $imagePath): string
6868
{
6969
$image = $this->productImageFactory->create();
7070
$image->setDestinationSubdir($imageType)

0 commit comments

Comments
 (0)