55 */
66namespace Magento \MediaGalleryCatalogUi \Ui \Component \Listing \Columns ;
77
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 ;
1011use Magento \Framework \View \Element \UiComponent \ContextInterface ;
1112use Magento \Framework \View \Element \UiComponentFactory ;
1213use Magento \Store \Model \Store ;
@@ -27,56 +28,87 @@ class Thumbnail extends Column
2728 /**
2829 * @var Image
2930 */
30- private $ imageHelper ;
31+ private $ categoryImage ;
3132
3233 /**
33- * @var ProductFactory
34+ * @var CategoryRepository
3435 */
35- private $ productFactory ;
36+ private $ categoryRepository ;
3637
3738 /**
39+ * @var AssetRepository
40+ */
41+ private $ assetRepository ;
42+
43+ /**
44+ * @var string[]
45+ */
46+ private $ defaultPlaceholder ;
47+
48+ /**
49+ * Thumbnail constructor.
3850 * @param ContextInterface $context
3951 * @param UiComponentFactory $uiComponentFactory
4052 * @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
4357 * @param array $components
4458 * @param array $data
4559 */
4660 public function __construct (
4761 ContextInterface $ context ,
4862 UiComponentFactory $ uiComponentFactory ,
4963 StoreManagerInterface $ storeManager ,
50- Image $ image ,
51- ProductFactory $ productFactory ,
64+ Image $ categoryImage ,
65+ CategoryRepository $ categoryRepository ,
66+ AssetRepository $ assetRepository ,
67+ array $ defaultPlaceholder = [],
5268 array $ components = [],
5369 array $ data = []
5470 ) {
5571 parent ::__construct ($ context , $ uiComponentFactory , $ components , $ data );
56- $ this ->imageHelper = $ image ;
5772 $ this ->storeManager = $ storeManager ;
58- $ this ->productFactory = $ productFactory ;
73+ $ this ->categoryImage = $ categoryImage ;
74+ $ this ->categoryRepository = $ categoryRepository ;
75+ $ this ->assetRepository = $ assetRepository ;
76+ $ this ->defaultPlaceholder = $ defaultPlaceholder ;
5977 }
6078
6179 /**
6280 * Prepare Data Source
6381 *
6482 * @param array $dataSource
6583 * @return array
84+ * @throws \Magento\Framework\Exception\LocalizedException
85+ * @throws \Magento\Framework\Exception\NoSuchEntityException
6686 */
6787 public function prepareDataSource (array $ dataSource )
6888 {
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 ;
78108 }
79109 }
110+
111+ $ item [$ fieldName . '_src ' ] = $ this ->assetRepository ->getUrl ($ this ->defaultPlaceholder ['image ' ]);
80112 }
81113
82114 return $ dataSource ;
0 commit comments