5
5
*/
6
6
namespace Magento \MediaGalleryCatalogUi \Ui \Component \Listing \Columns ;
7
7
8
- use Magento \Catalog \Helper \Image ;
9
- use Magento \Framework \DataObject ;
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,48 +28,87 @@ class Thumbnail extends Column
27
28
/**
28
29
* @var Image
29
30
*/
30
- private $ imageHelper ;
31
+ private $ categoryImage ;
31
32
32
33
/**
34
+ * @var CategoryRepository
35
+ */
36
+ private $ categoryRepository ;
37
+
38
+ /**
39
+ * @var AssetRepository
40
+ */
41
+ private $ assetRepository ;
42
+
43
+ /**
44
+ * @var string[]
45
+ */
46
+ private $ defaultPlaceholder ;
47
+
48
+ /**
49
+ * Thumbnail constructor.
33
50
* @param ContextInterface $context
34
51
* @param UiComponentFactory $uiComponentFactory
35
52
* @param StoreManagerInterface $storeManager
36
- * @param Image $image
53
+ * @param Image $categoryImage
54
+ * @param CategoryRepository $categoryRepository
55
+ * @param AssetRepository $assetRepository
56
+ * @param array $defaultPlaceholder
37
57
* @param array $components
38
58
* @param array $data
39
59
*/
40
60
public function __construct (
41
61
ContextInterface $ context ,
42
62
UiComponentFactory $ uiComponentFactory ,
43
63
StoreManagerInterface $ storeManager ,
44
- Image $ image ,
64
+ Image $ categoryImage ,
65
+ CategoryRepository $ categoryRepository ,
66
+ AssetRepository $ assetRepository ,
67
+ array $ defaultPlaceholder = [],
45
68
array $ components = [],
46
69
array $ data = []
47
70
) {
48
71
parent ::__construct ($ context , $ uiComponentFactory , $ components , $ data );
49
- $ this ->imageHelper = $ image ;
50
72
$ this ->storeManager = $ storeManager ;
73
+ $ this ->categoryImage = $ categoryImage ;
74
+ $ this ->categoryRepository = $ categoryRepository ;
75
+ $ this ->assetRepository = $ assetRepository ;
76
+ $ this ->defaultPlaceholder = $ defaultPlaceholder ;
51
77
}
52
78
53
79
/**
54
80
* Prepare Data Source
55
81
*
56
82
* @param array $dataSource
57
83
* @return array
84
+ * @throws \Magento\Framework\Exception\LocalizedException
85
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
58
86
*/
59
87
public function prepareDataSource (array $ dataSource )
60
88
{
61
- if (isset ($ dataSource ['data ' ]['items ' ])) {
62
- $ fieldName = $ this ->getData ('name ' );
63
- foreach ($ dataSource ['data ' ]['items ' ] as & $ item ) {
64
- if (isset ($ item [$ fieldName ])) {
65
- $ item [$ fieldName . '_src ' ] = $ this ->getUrl ($ item [$ fieldName ]);
66
- } else {
67
- $ category = new DataObject ($ item );
68
- $ imageHelper = $ this ->imageHelper ->init ($ category , 'product_listing_thumbnail ' );
69
- $ 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 ;
70
108
}
71
109
}
110
+
111
+ $ item [$ fieldName . '_src ' ] = $ this ->assetRepository ->getUrl ($ this ->defaultPlaceholder ['image ' ]);
72
112
}
73
113
74
114
return $ dataSource ;
0 commit comments