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