3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Ui \DataProvider \Product \Form \Modifier ;
7
9
8
10
use Magento \Catalog \Model \Locator \LocatorInterface ;
11
13
use Magento \Framework \App \CacheInterface ;
12
14
use Magento \Framework \DB \Helper as DbHelper ;
13
15
use Magento \Catalog \Model \Category as CategoryModel ;
16
+ use Magento \Framework \Exception \LocalizedException ;
14
17
use Magento \Framework \Serialize \SerializerInterface ;
15
18
use Magento \Framework \UrlInterface ;
16
19
use Magento \Framework \Stdlib \ArrayManager ;
@@ -202,6 +205,7 @@ protected function createNewCategoryModal(array $meta)
202
205
*
203
206
* @param array $meta
204
207
* @return array
208
+ * @throws LocalizedException
205
209
* @since 101.0.0
206
210
*/
207
211
protected function customizeCategoriesField (array $ meta )
@@ -306,20 +310,64 @@ protected function customizeCategoriesField(array $meta)
306
310
*
307
311
* @param string|null $filter
308
312
* @return array
313
+ * @throws LocalizedException
309
314
* @since 101.0.0
310
315
*/
311
316
protected function getCategoriesTree ($ filter = null )
312
317
{
313
- $ categoryTree = $ this ->getCacheManager ()->load (self ::CATEGORY_TREE_ID . '_ ' . $ filter );
314
- if ($ categoryTree ) {
315
- return $ this ->serializer ->unserialize ($ categoryTree );
318
+ $ storeId = (int ) $ this ->locator ->getStore ()->getId ();
319
+
320
+ $ cachedCategoriesTree = $ this ->getCacheManager ()
321
+ ->load ($ this ->getCategoriesTreeCacheId ($ storeId , (string ) $ filter ));
322
+ if (!empty ($ cachedCategoriesTree )) {
323
+ return $ this ->serializer ->unserialize ($ cachedCategoriesTree );
316
324
}
317
325
318
- $ storeId = $ this ->locator ->getStore ()->getId ();
326
+ $ categoriesTree = $ this ->retrieveCategoriesTree (
327
+ $ storeId ,
328
+ $ this ->retrieveShownCategoriesIds ($ storeId , (string ) $ filter )
329
+ );
330
+
331
+ $ this ->getCacheManager ()->save (
332
+ $ this ->serializer ->serialize ($ categoriesTree ),
333
+ $ this ->getCategoriesTreeCacheId ($ storeId , (string ) $ filter ),
334
+ [
335
+ \Magento \Catalog \Model \Category::CACHE_TAG ,
336
+ \Magento \Framework \App \Cache \Type \Block::CACHE_TAG
337
+ ]
338
+ );
339
+
340
+ return $ categoriesTree ;
341
+ }
342
+
343
+ /**
344
+ * Get cache id for categories tree.
345
+ *
346
+ * @param int $storeId
347
+ * @param string $filter
348
+ * @return string
349
+ */
350
+ private function getCategoriesTreeCacheId (int $ storeId , string $ filter = '' ) : string
351
+ {
352
+ return self ::CATEGORY_TREE_ID
353
+ . '_ ' . (string ) $ storeId
354
+ . '_ ' . $ filter ;
355
+ }
356
+
357
+ /**
358
+ * Retrieve filtered list of categories id.
359
+ *
360
+ * @param int $storeId
361
+ * @param string $filter
362
+ * @return array
363
+ * @throws LocalizedException
364
+ */
365
+ private function retrieveShownCategoriesIds (int $ storeId , string $ filter = '' ) : array
366
+ {
319
367
/* @var $matchingNamesCollection \Magento\Catalog\Model\ResourceModel\Category\Collection */
320
368
$ matchingNamesCollection = $ this ->categoryCollectionFactory ->create ();
321
369
322
- if ($ filter !== null ) {
370
+ if (! empty ( $ filter) ) {
323
371
$ matchingNamesCollection ->addAttributeToFilter (
324
372
'name ' ,
325
373
['like ' => $ this ->dbHelper ->addLikeEscape ($ filter , ['position ' => 'any ' ])]
@@ -339,6 +387,19 @@ protected function getCategoriesTree($filter = null)
339
387
}
340
388
}
341
389
390
+ return $ shownCategoriesIds ;
391
+ }
392
+
393
+ /**
394
+ * Retrieve tree of categories with attributes.
395
+ *
396
+ * @param int $storeId
397
+ * @param array $shownCategoriesIds
398
+ * @return array
399
+ * @throws LocalizedException
400
+ */
401
+ private function retrieveCategoriesTree (int $ storeId , array $ shownCategoriesIds ) : array
402
+ {
342
403
/* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
343
404
$ collection = $ this ->categoryCollectionFactory ->create ();
344
405
@@ -365,15 +426,6 @@ protected function getCategoriesTree($filter = null)
365
426
$ categoryById [$ category ->getParentId ()]['optgroup ' ][] = &$ categoryById [$ category ->getId ()];
366
427
}
367
428
368
- $ this ->getCacheManager ()->save (
369
- $ this ->serializer ->serialize ($ categoryById [CategoryModel::TREE_ROOT_ID ]['optgroup ' ]),
370
- self ::CATEGORY_TREE_ID . '_ ' . $ filter ,
371
- [
372
- \Magento \Catalog \Model \Category::CACHE_TAG ,
373
- \Magento \Framework \App \Cache \Type \Block::CACHE_TAG
374
- ]
375
- );
376
-
377
429
return $ categoryById [CategoryModel::TREE_ROOT_ID ]['optgroup ' ];
378
430
}
379
431
}
0 commit comments