Skip to content

Commit 4745f33

Browse files
committed
MC-15991: Category image return only image name
1 parent 3e0ec20 commit 4745f33

File tree

1 file changed

+20
-1
lines changed
  • app/code/Magento/CatalogGraphQl/Model/Resolver/Category

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@
1212
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1313
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Store\Api\Data\StoreInterface;
15+
use Magento\Framework\Filesystem\DirectoryList;
1516

1617
/**
1718
* Resolve category image to a fully qualified URL
1819
*/
1920
class Image implements ResolverInterface
2021
{
22+
/** @var DirectoryList */
23+
private $directoryList;
24+
25+
/**
26+
* @param DirectoryList $directoryList
27+
*/
28+
public function __construct(DirectoryList $directoryList)
29+
{
30+
$this->directoryList = $directoryList;
31+
}
32+
2133
/**
2234
* @inheritdoc
2335
*/
@@ -31,14 +43,21 @@ public function resolve(
3143
if (!isset($value['model'])) {
3244
throw new LocalizedException(__('"model" value should be specified'));
3345
}
46+
/** @var \Magento\Catalog\Model\Category $category */
3447
$category = $value['model'];
3548
$imagePath = $category->getImage();
3649
if (empty($imagePath)) {
3750
return null;
3851
}
3952
/** @var StoreInterface $store */
4053
$store = $context->getExtensionAttributes()->getStore();
41-
$baseUrl = $store->getBaseUrl();
54+
$baseUrl = $store->getBaseUrl('media');
55+
56+
$mediaPath = $this->directoryList->getUrlPath('media');
57+
$pos = strpos($imagePath, $mediaPath);
58+
if ($pos !== false) {
59+
$imagePath = substr($imagePath, $pos + strlen($mediaPath), strlen($baseUrl));
60+
}
4261
$imageUrl = rtrim($baseUrl, '/') . '/' . ltrim($imagePath, '/');
4362

4463
return $imageUrl;

0 commit comments

Comments
 (0)