Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/Observer/PrepareMenuItems.php b/Observer/PrepareMenuItems.php
- index 66d002e..d836cf5 100644
- --- a/Observer/PrepareMenuItems.php
- +++ b/Observer/PrepareMenuItems.php
- @@ -104,11 +104,6 @@ public function execute(\Magento\Framework\Event\Observer $observer)
- $parentItemNode->addChild($itemNode);
- $mapping[$item->getId()] = $itemNode;
- -
- - // $block->addIdentity(Item::CACHE_TAG . '_' . $item->getId());
- - if ($item->isCategoryItem()) {
- - $block->addIdentity(Category::CACHE_TAG . '_' . $item->getRemoteEntityId());
- - }
- }
- }
- diff --git a/Observer/SaveCategory.php b/Observer/SaveCategory.php
- index e91a4a5..e623da2 100644
- --- a/Observer/SaveCategory.php
- +++ b/Observer/SaveCategory.php
- @@ -5,9 +5,15 @@
- use Magento\Store\Model\Store;
- use Magento\Framework\Event\ObserverInterface;
- use Swissup\Navigationpro\Model\Item;
- +use Swissup\Navigationpro\Model\Menu;
- class SaveCategory implements ObserverInterface
- {
- + /**
- + * @var \Magento\Framework\App\CacheInterface
- + */
- + private $cache;
- +
- /**
- * @var \Swissup\Navigationpro\Model\ItemFactory
- */
- @@ -24,15 +30,18 @@ class SaveCategory implements ObserverInterface
- private $categoryRepository;
- /**
- + * @param \Magento\Framework\App\CacheInterface $cache
- * @param \Swissup\Navigationpro\Model\ItemFactory $itemFactory
- * @param \Swissup\Navigationpro\Model\ResourceModel\Item\CollectionFactory $itemCollectionFactory
- * @param \Magento\Catalog\Model\CategoryRepository $categoryRepository
- */
- public function __construct(
- + \Magento\Framework\App\CacheInterface $cache,
- \Swissup\Navigationpro\Model\ItemFactory $itemFactory,
- \Swissup\Navigationpro\Model\ResourceModel\Item\CollectionFactory $itemCollectionFactory,
- \Magento\Catalog\Model\CategoryRepository $categoryRepository
- ) {
- + $this->cache = $cache;
- $this->itemFactory = $itemFactory;
- $this->itemCollectionFactory = $itemCollectionFactory;
- $this->categoryRepository = $categoryRepository;
- @@ -47,9 +56,6 @@ public function __construct(
- public function execute(\Magento\Framework\Event\Observer $observer)
- {
- $category = $observer->getEvent()->getCategory();
- - if (!$category->isObjectNew()) {
- - return;
- - }
- /** @var \Swissup\Navigationpro\Model\ResourceModel\Item\Collection $items */
- $items = $this->itemCollectionFactory->create()
- @@ -58,7 +64,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
- if ($category->getLevel() > 2) {
- $items->addFieldToFilter('remote_entity_id', $category->getParentId());
- - $this->createNestedItems($items, $category);
- + if ($category->isObjectNew()) {
- + $this->createNestedItems($items, $category);
- + } else {
- + $this->updateMenuCache($items->getColumnValues('menu_id'));
- + }
- } else {
- $siblings = $this->categoryRepository
- ->get($category->getParentId())
- @@ -75,7 +85,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
- ->getSelect()
- ->group('menu_id');
- - $this->createRootItems($items->getColumnValues('menu_id'), $category);
- + if ($category->isObjectNew()) {
- + $this->createRootItems($items->getColumnValues('menu_id'), $category);
- + } else {
- + $this->updateMenuCache($items->getColumnValues('menu_id'));
- + }
- }
- }
- @@ -127,4 +141,15 @@ protected function getItem($remoteEntity, $menuId, $parentItem = null)
- return $newItem;
- }
- +
- + protected function updateMenuCache($menuIds)
- + {
- + $tags = [];
- +
- + foreach ($menuIds as $menuId) {
- + $tags[] = Menu::CACHE_TAG . '_' . $menuId;
- + }
- +
- + $this->cache->clean($tags);
- + }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement