|
7 | 7 |
|
8 | 8 | namespace Magento\UrlRewrite\Model;
|
9 | 9 |
|
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Model\Category; |
| 13 | +use Magento\Catalog\Model\Product; |
| 14 | +use Magento\Cms\Api\PageRepositoryInterface; |
| 15 | +use Magento\Cms\Model\Page; |
10 | 16 | use Magento\Framework\App\ObjectManager;
|
| 17 | +use Magento\Framework\EntityManager\EventManager; |
| 18 | +use Magento\Framework\Indexer\CacheContext; |
11 | 19 | use Magento\Framework\Serialize\Serializer\Json;
|
| 20 | +use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite; |
12 | 21 |
|
13 | 22 | /**
|
14 | 23 | * UrlRewrite model class
|
@@ -92,4 +101,58 @@ public function setMetadata($metadata)
|
92 | 101 | }
|
93 | 102 | return $this->setData(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::METADATA, $metadata);
|
94 | 103 | }
|
| 104 | + |
| 105 | + private function opt1() { |
| 106 | + $map = [ |
| 107 | + Rewrite::ENTITY_TYPE_PRODUCT => Product::CACHE_TAG, |
| 108 | + Rewrite::ENTITY_TYPE_CATEGORY => Category::CACHE_TAG, |
| 109 | + Rewrite::ENTITY_TYPE_CMS_PAGE => Page::CACHE_TAG |
| 110 | + ]; |
| 111 | + |
| 112 | + if ($this->getEntityType() !== Rewrite::ENTITY_TYPE_CUSTOM) { |
| 113 | + $cacheKey = $map[$this->getEntityType()]; |
| 114 | + |
| 115 | + $cacheContext = ObjectManager::getInstance()->get(CacheContext::class); |
| 116 | + $eventManager = ObjectManager::getInstance()->get(EventManager::class); |
| 117 | + |
| 118 | + $cacheContext->registerEntities($cacheKey, [$this->getEntityId()]); |
| 119 | + $eventManager->dispatch('clean_cache_by_tags', ['object' => $cacheContext]); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private function opt2() { |
| 124 | + $map = [ |
| 125 | + Rewrite::ENTITY_TYPE_PRODUCT => function ($prodId) { |
| 126 | + /** @var ProductRepositoryInterface $productRepository */ |
| 127 | + $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); |
| 128 | + return $productRepository->getById($prodId); |
| 129 | + }, |
| 130 | + Rewrite::ENTITY_TYPE_CATEGORY => function ($catId) { |
| 131 | + /** @var CategoryRepositoryInterface $productRepository */ |
| 132 | + $categoryRepository = ObjectManager::getInstance()->get(CategoryRepositoryInterface::class); |
| 133 | + return $categoryRepository->get($catId); |
| 134 | + }, |
| 135 | + Rewrite::ENTITY_TYPE_CMS_PAGE => function ($cmsId) { |
| 136 | + /** @var PageRepositoryInterface $productRepository */ |
| 137 | + $pageRepository = ObjectManager::getInstance()->get(PageRepositoryInterface::class); |
| 138 | + return $pageRepository->getById($cmsId); |
| 139 | + }, |
| 140 | + Rewrite::ENTITY_TYPE_CUSTOM => false |
| 141 | + ]; |
| 142 | + |
| 143 | + $getter = $map[$this->getEntityType()]; |
| 144 | + |
| 145 | + if ($getter) { |
| 146 | + $entity = $getter($this->getEntityId()); |
| 147 | + |
| 148 | + $entityManager = ObjectManager::getInstance()->get(EventManager::class); |
| 149 | + $entityManager->dispatch('clean_cache_by_tags', ['object' => $entity]); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public function afterSave() |
| 154 | + { |
| 155 | + $this->opt1(); |
| 156 | + return parent::afterSave(); // TODO: Change the autogenerated stub |
| 157 | + } |
95 | 158 | }
|
0 commit comments