12
12
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
13
13
use Magento \Framework \Exception \LocalizedException ;
14
14
use Magento \Store \Api \Data \StoreInterface ;
15
+ use Magento \Framework \Filesystem \DirectoryList ;
15
16
16
17
/**
17
18
* Resolve category image to a fully qualified URL
18
19
*/
19
20
class Image implements ResolverInterface
20
21
{
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
+
21
33
/**
22
34
* @inheritdoc
23
35
*/
@@ -31,14 +43,21 @@ public function resolve(
31
43
if (!isset ($ value ['model ' ])) {
32
44
throw new LocalizedException (__ ('"model" value should be specified ' ));
33
45
}
46
+ /** @var \Magento\Catalog\Model\Category $category */
34
47
$ category = $ value ['model ' ];
35
48
$ imagePath = $ category ->getImage ();
36
49
if (empty ($ imagePath )) {
37
50
return null ;
38
51
}
39
52
/** @var StoreInterface $store */
40
53
$ 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
+ }
42
61
$ imageUrl = rtrim ($ baseUrl , '/ ' ) . '/ ' . ltrim ($ imagePath , '/ ' );
43
62
44
63
return $ imageUrl ;
0 commit comments