@@ -15,7 +15,9 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma
15
15
{
16
16
const DEFAULT_CACHE_TAG = 'MAGEPOW_CATEGORIES ' ;
17
17
18
- const XML_PATH = 'category_page ' ;
18
+ const XML_PATH = 'category_page ' ;
19
+
20
+ const MEDIA_PATH = 'catalog/category ' ;
19
21
20
22
public $ helper ;
21
23
@@ -28,23 +30,35 @@ class Categories extends \Magento\Framework\View\Element\Template implements \Ma
28
30
public $ categoryFactory ;
29
31
30
32
public $ catalogHelperOutput ;
33
+
34
+ /**
35
+ * @var \Magento\Framework\Image\AdapterFactory
36
+ */
37
+ protected $ _imageFactory ;
38
+
39
+ protected $ _filesystem ;
31
40
32
41
public function __construct (
33
- \Magento \Store \Model \StoreManagerInterface $ storeManager ,
42
+ \Magento \Framework \View \Element \Template \Context $ context ,
43
+ \Magento \Framework \Image \AdapterFactory $ imageFactory ,
34
44
\Magento \Framework \View \Asset \Repository $ viewAssetRepo ,
35
45
\Magento \Framework \Registry $ coreRegistry ,
36
46
\Magento \Catalog \Model \CategoryFactory $ categoryFactory ,
37
47
\Magento \Catalog \Helper \Output $ catalogHelperOutput ,
48
+ \Magento \Store \Model \StoreManagerInterface $ storeManager ,
38
49
\Magepow \Categories \Helper \Data $ helper ,
39
- \Magento \Framework \View \Element \Template \Context $ context ,
40
50
array $ data = []
41
51
) {
42
52
$ this ->storeManager = $ storeManager ;
43
53
$ this ->viewAssetRepo = $ viewAssetRepo ;
44
54
$ this ->coreRegistry = $ coreRegistry ;
45
55
$ this ->categoryFactory = $ categoryFactory ;
46
56
$ this ->catalogHelperOutput = $ catalogHelperOutput ;
47
- $ this ->helper = $ helper ;
57
+ $ this ->_imageFactory = $ imageFactory ;
58
+ $ this ->_filesystem = $ context ->getFilesystem ();
59
+ $ this ->_directoryRed = $ this ->_filesystem ->getDirectoryRead (\Magento \Framework \App \Filesystem \DirectoryList::MEDIA );
60
+ $ this ->helper = $ helper ;
61
+
48
62
parent ::__construct ($ context , $ data );
49
63
}
50
64
@@ -134,24 +148,32 @@ public function getDescription($category)
134
148
return trim ($ categoryDescription );
135
149
}
136
150
137
- public function getImage ($ category )
151
+ public function getImage ($ image )
138
152
{
139
- $ placeholderImageUrl = $ this ->viewAssetRepo ->getUrl (
140
- 'Magento_Catalog::images/product/placeholder/small_image.jpg '
141
- );
142
- $ image = $ category ->getImage ();
143
- if ($ image != null ) {
144
- $ url = $ this ->getImageUrl ($ image );
153
+ return $ this ->getImageUrl ($ image );
154
+ }
155
+
156
+ public function getImageInfo ($ image )
157
+ {
158
+ if (is_object ($ image )){
159
+ $ img = $ image ->getImage ();
160
+ if (!$ img ) return $ image ;
145
161
} else {
146
- $ url = $ placeholderImageUrl ;
147
- }
148
- return $ url ;
162
+ $ img = $ image ;
163
+ }
164
+ $ _image = $ this ->_imageFactory ->create ();
165
+ $ absPath = $ this ->_directoryRed ->getAbsolutePath () . str_replace ('/pub/media/ ' , '' , $ img );
166
+ if (file_exists ($ absPath ) ){
167
+ $ _image ->open ($ absPath );
168
+ return $ _image ;
169
+ }
170
+ return $ image ;
149
171
}
150
-
172
+
151
173
public function getImageUrl ($ image )
152
174
{
153
- $ url = false ;
154
- if ($ image ) {
175
+ if ( is_object ( $ image )) $ image = $ image -> getImage ();
176
+ if ($ image ) {
155
177
if (substr ($ image , 0 , 1 ) === '/ ' ) {
156
178
$ url = $ this ->storeManager ->getStore ()->getBaseUrl (
157
179
\Magento \Framework \UrlInterface::URL_TYPE_WEB
@@ -161,7 +183,9 @@ public function getImageUrl($image)
161
183
\Magento \Framework \UrlInterface::URL_TYPE_MEDIA
162
184
) . 'catalog/category/ ' . $ image ;
163
185
}
164
- }
186
+ } else {
187
+ $ url = $ this ->viewAssetRepo ->getUrl ('Magento_Catalog::images/product/placeholder/small_image.jpg ' );
188
+ }
165
189
return $ url ;
166
190
}
167
191
0 commit comments