Guest User

Untitled

a guest
Dec 12th, 2019
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git a/Observer/PrepareMenuItems.php b/Observer/PrepareMenuItems.php
  2. index 66d002e..d836cf5 100644
  3. --- a/Observer/PrepareMenuItems.php
  4. +++ b/Observer/PrepareMenuItems.php
  5. @@ -104,11 +104,6 @@ public function execute(\Magento\Framework\Event\Observer $observer)
  6. $parentItemNode->addChild($itemNode);
  7.  
  8. $mapping[$item->getId()] = $itemNode;
  9. -
  10. - // $block->addIdentity(Item::CACHE_TAG . '_' . $item->getId());
  11. - if ($item->isCategoryItem()) {
  12. - $block->addIdentity(Category::CACHE_TAG . '_' . $item->getRemoteEntityId());
  13. - }
  14. }
  15. }
  16.  
  17. diff --git a/Observer/SaveCategory.php b/Observer/SaveCategory.php
  18. index e91a4a5..e623da2 100644
  19. --- a/Observer/SaveCategory.php
  20. +++ b/Observer/SaveCategory.php
  21. @@ -5,9 +5,15 @@
  22. use Magento\Store\Model\Store;
  23. use Magento\Framework\Event\ObserverInterface;
  24. use Swissup\Navigationpro\Model\Item;
  25. +use Swissup\Navigationpro\Model\Menu;
  26.  
  27. class SaveCategory implements ObserverInterface
  28. {
  29. + /**
  30. + * @var \Magento\Framework\App\CacheInterface
  31. + */
  32. + private $cache;
  33. +
  34. /**
  35. * @var \Swissup\Navigationpro\Model\ItemFactory
  36. */
  37. @@ -24,15 +30,18 @@ class SaveCategory implements ObserverInterface
  38. private $categoryRepository;
  39.  
  40. /**
  41. + * @param \Magento\Framework\App\CacheInterface $cache
  42. * @param \Swissup\Navigationpro\Model\ItemFactory $itemFactory
  43. * @param \Swissup\Navigationpro\Model\ResourceModel\Item\CollectionFactory $itemCollectionFactory
  44. * @param \Magento\Catalog\Model\CategoryRepository $categoryRepository
  45. */
  46. public function __construct(
  47. + \Magento\Framework\App\CacheInterface $cache,
  48. \Swissup\Navigationpro\Model\ItemFactory $itemFactory,
  49. \Swissup\Navigationpro\Model\ResourceModel\Item\CollectionFactory $itemCollectionFactory,
  50. \Magento\Catalog\Model\CategoryRepository $categoryRepository
  51. ) {
  52. + $this->cache = $cache;
  53. $this->itemFactory = $itemFactory;
  54. $this->itemCollectionFactory = $itemCollectionFactory;
  55. $this->categoryRepository = $categoryRepository;
  56. @@ -47,9 +56,6 @@ public function __construct(
  57. public function execute(\Magento\Framework\Event\Observer $observer)
  58. {
  59. $category = $observer->getEvent()->getCategory();
  60. - if (!$category->isObjectNew()) {
  61. - return;
  62. - }
  63.  
  64. /** @var \Swissup\Navigationpro\Model\ResourceModel\Item\Collection $items */
  65. $items = $this->itemCollectionFactory->create()
  66. @@ -58,7 +64,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
  67. if ($category->getLevel() > 2) {
  68. $items->addFieldToFilter('remote_entity_id', $category->getParentId());
  69.  
  70. - $this->createNestedItems($items, $category);
  71. + if ($category->isObjectNew()) {
  72. + $this->createNestedItems($items, $category);
  73. + } else {
  74. + $this->updateMenuCache($items->getColumnValues('menu_id'));
  75. + }
  76. } else {
  77. $siblings = $this->categoryRepository
  78. ->get($category->getParentId())
  79. @@ -75,7 +85,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
  80. ->getSelect()
  81. ->group('menu_id');
  82.  
  83. - $this->createRootItems($items->getColumnValues('menu_id'), $category);
  84. + if ($category->isObjectNew()) {
  85. + $this->createRootItems($items->getColumnValues('menu_id'), $category);
  86. + } else {
  87. + $this->updateMenuCache($items->getColumnValues('menu_id'));
  88. + }
  89. }
  90. }
  91.  
  92. @@ -127,4 +141,15 @@ protected function getItem($remoteEntity, $menuId, $parentItem = null)
  93.  
  94. return $newItem;
  95. }
  96. +
  97. + protected function updateMenuCache($menuIds)
  98. + {
  99. + $tags = [];
  100. +
  101. + foreach ($menuIds as $menuId) {
  102. + $tags[] = Menu::CACHE_TAG . '_' . $menuId;
  103. + }
  104. +
  105. + $this->cache->clean($tags);
  106. + }
  107. }
RAW Paste Data