5
5
*/
6
6
namespace Magento \MediaGalleryCatalogUi \Ui \Component \Listing \Columns ;
7
7
8
- use Magento \Catalog \Helper \Image ;
9
- use Magento \Catalog \Model \ProductFactory ;
8
+ use Magento \Catalog \Model \Category \Image ;
9
+ use Magento \Catalog \Model \CategoryRepository ;
10
+ use Magento \Framework \View \Asset \Repository as AssetRepository ;
10
11
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
11
12
use Magento \Framework \View \Element \UiComponentFactory ;
12
13
use Magento \Store \Model \Store ;
@@ -27,56 +28,87 @@ class Thumbnail extends Column
27
28
/**
28
29
* @var Image
29
30
*/
30
- private $ imageHelper ;
31
+ private $ categoryImage ;
31
32
32
33
/**
33
- * @var ProductFactory
34
+ * @var CategoryRepository
34
35
*/
35
- private $ productFactory ;
36
+ private $ categoryRepository ;
36
37
37
38
/**
39
+ * @var AssetRepository
40
+ */
41
+ private $ assetRepository ;
42
+
43
+ /**
44
+ * @var string[]
45
+ */
46
+ private $ defaultPlaceholder ;
47
+
48
+ /**
49
+ * Thumbnail constructor.
38
50
* @param ContextInterface $context
39
51
* @param UiComponentFactory $uiComponentFactory
40
52
* @param StoreManagerInterface $storeManager
41
- * @param Image $image
42
- * @param ProductFactory $productFactory
53
+ * @param Image $categoryImage
54
+ * @param CategoryRepository $categoryRepository
55
+ * @param AssetRepository $assetRepository
56
+ * @param array $defaultPlaceholder
43
57
* @param array $components
44
58
* @param array $data
45
59
*/
46
60
public function __construct (
47
61
ContextInterface $ context ,
48
62
UiComponentFactory $ uiComponentFactory ,
49
63
StoreManagerInterface $ storeManager ,
50
- Image $ image ,
51
- ProductFactory $ productFactory ,
64
+ Image $ categoryImage ,
65
+ CategoryRepository $ categoryRepository ,
66
+ AssetRepository $ assetRepository ,
67
+ array $ defaultPlaceholder = [],
52
68
array $ components = [],
53
69
array $ data = []
54
70
) {
55
71
parent ::__construct ($ context , $ uiComponentFactory , $ components , $ data );
56
- $ this ->imageHelper = $ image ;
57
72
$ this ->storeManager = $ storeManager ;
58
- $ this ->productFactory = $ productFactory ;
73
+ $ this ->categoryImage = $ categoryImage ;
74
+ $ this ->categoryRepository = $ categoryRepository ;
75
+ $ this ->assetRepository = $ assetRepository ;
76
+ $ this ->defaultPlaceholder = $ defaultPlaceholder ;
59
77
}
60
78
61
79
/**
62
80
* Prepare Data Source
63
81
*
64
82
* @param array $dataSource
65
83
* @return array
84
+ * @throws \Magento\Framework\Exception\LocalizedException
85
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
66
86
*/
67
87
public function prepareDataSource (array $ dataSource )
68
88
{
69
- if (isset ($ dataSource ['data ' ]['items ' ])) {
70
- $ fieldName = $ this ->getData ('name ' );
71
- foreach ($ dataSource ['data ' ]['items ' ] as & $ item ) {
72
- if (isset ($ item [$ fieldName ])) {
73
- $ item [$ fieldName . '_src ' ] = $ this ->getUrl ($ item [$ fieldName ]);
74
- } else {
75
- $ category = $ this ->productFactory ->create (['data ' => $ item ]);
76
- $ imageHelper = $ this ->imageHelper ->init ($ category , 'product_listing_thumbnail ' );
77
- $ item [$ fieldName . '_src ' ] = $ imageHelper ->getUrl ();
89
+ if (!isset ($ dataSource ['data ' ]['items ' ])) {
90
+ return $ dataSource ;
91
+ }
92
+
93
+ $ fieldName = $ this ->getData ('name ' );
94
+ foreach ($ dataSource ['data ' ]['items ' ] as & $ item ) {
95
+ if (isset ($ item [$ fieldName ])) {
96
+ $ item [$ fieldName . '_src ' ] = $ this ->getUrl ($ item [$ fieldName ]);
97
+ continue ;
98
+ }
99
+
100
+ if (isset ($ item ['entity_id ' ])) {
101
+ $ src = $ this ->categoryImage ->getUrl (
102
+ $ this ->categoryRepository ->get ($ item ['entity_id ' ])
103
+ );
104
+
105
+ if (!empty ($ src )) {
106
+ $ item [$ fieldName . '_src ' ] = $ src ;
107
+ continue ;
78
108
}
79
109
}
110
+
111
+ $ item [$ fieldName . '_src ' ] = $ this ->assetRepository ->getUrl ($ this ->defaultPlaceholder ['image ' ]);
80
112
}
81
113
82
114
return $ dataSource ;
0 commit comments