13
13
use Magento \Framework \GraphQl \Config \Element \Field ;
14
14
use Magento \Framework \GraphQl \Query \ResolverInterface ;
15
15
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
16
+ use Magento \Framework \View \Asset \Repository as AssetRepository ;
16
17
17
18
/**
18
19
* Returns product's image url
@@ -23,14 +24,22 @@ class Url implements ResolverInterface
23
24
* @var ImageFactory
24
25
*/
25
26
private $ productImageFactory ;
27
+ /**
28
+ * @var AssetRepository
29
+ */
30
+ private $ assetRepository ;
26
31
27
32
/**
33
+ * Url constructor.
28
34
* @param ImageFactory $productImageFactory
35
+ * @param AssetRepository $assetRepository
29
36
*/
30
37
public function __construct (
31
- ImageFactory $ productImageFactory
38
+ ImageFactory $ productImageFactory ,
39
+ AssetRepository $ assetRepository
32
40
) {
33
41
$ this ->productImageFactory = $ productImageFactory ;
42
+ $ this ->assetRepository = $ assetRepository ;
34
43
}
35
44
36
45
/**
@@ -55,8 +64,7 @@ public function resolve(
55
64
$ product = $ value ['model ' ];
56
65
$ imagePath = $ product ->getData ($ value ['image_type ' ]);
57
66
58
- $ imageUrl = $ this ->getImageUrl ($ value ['image_type ' ], $ imagePath );
59
- return $ imageUrl ;
67
+ return $ this ->getImageUrl ($ value ['image_type ' ], $ imagePath );
60
68
}
61
69
62
70
/**
@@ -71,7 +79,15 @@ private function getImageUrl(string $imageType, ?string $imagePath): string
71
79
$ image = $ this ->productImageFactory ->create ();
72
80
$ image ->setDestinationSubdir ($ imageType )
73
81
->setBaseFile ($ imagePath );
74
- $ imageUrl = $ image ->getUrl ();
82
+
83
+ $ imageUrl = $ image ->isBaseFilePlaceholder ()
84
+ ? $ this ->assetRepository ->createAsset (
85
+ "Magento_Catalog::images/product/placeholder/ {$ imageType }.jpg " ,
86
+ ['area ' => \Magento \Framework \App \Area::AREA_FRONTEND ]
87
+ )
88
+ ->getUrl ()
89
+ : $ image ->getUrl ();
90
+
75
91
return $ imageUrl ;
76
92
}
77
93
}
0 commit comments