55 */
66namespace Magento \MediaGalleryCatalogUi \Ui \Component \Listing \Columns ;
77
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 ;
1011use Magento \Framework \View \Element \UiComponent \ContextInterface ;
1112use Magento \Framework \View \Element \UiComponentFactory ;
1213use Magento \Store \Model \Store ;
@@ -27,48 +28,87 @@ class Thumbnail extends Column
2728 /**
2829 * @var Image
2930 */
30- private $ imageHelper ;
31+ private $ categoryImage ;
3132
3233 /**
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.
3350 * @param ContextInterface $context
3451 * @param UiComponentFactory $uiComponentFactory
3552 * @param StoreManagerInterface $storeManager
36- * @param Image $image
53+ * @param Image $categoryImage
54+ * @param CategoryRepository $categoryRepository
55+ * @param AssetRepository $assetRepository
56+ * @param array $defaultPlaceholder
3757 * @param array $components
3858 * @param array $data
3959 */
4060 public function __construct (
4161 ContextInterface $ context ,
4262 UiComponentFactory $ uiComponentFactory ,
4363 StoreManagerInterface $ storeManager ,
44- Image $ image ,
64+ Image $ categoryImage ,
65+ CategoryRepository $ categoryRepository ,
66+ AssetRepository $ assetRepository ,
67+ array $ defaultPlaceholder = [],
4568 array $ components = [],
4669 array $ data = []
4770 ) {
4871 parent ::__construct ($ context , $ uiComponentFactory , $ components , $ data );
49- $ this ->imageHelper = $ image ;
5072 $ this ->storeManager = $ storeManager ;
73+ $ this ->categoryImage = $ categoryImage ;
74+ $ this ->categoryRepository = $ categoryRepository ;
75+ $ this ->assetRepository = $ assetRepository ;
76+ $ this ->defaultPlaceholder = $ defaultPlaceholder ;
5177 }
5278
5379 /**
5480 * Prepare Data Source
5581 *
5682 * @param array $dataSource
5783 * @return array
84+ * @throws \Magento\Framework\Exception\LocalizedException
85+ * @throws \Magento\Framework\Exception\NoSuchEntityException
5886 */
5987 public function prepareDataSource (array $ dataSource )
6088 {
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 ;
70108 }
71109 }
110+
111+ $ item [$ fieldName . '_src ' ] = $ this ->assetRepository ->getUrl ($ this ->defaultPlaceholder ['image ' ]);
72112 }
73113
74114 return $ dataSource ;
0 commit comments