Skip to content

Commit 2c9cc60

Browse files
committed
MAGETWO-91437: Invalidate indexer after category is deleted
- Write plugin to invalidate fulltext - Invalidate category_product on after delete for Category resource model - Write integration tests
1 parent 0ad656d commit 2c9cc60

File tree

7 files changed

+302
-110
lines changed

7 files changed

+302
-110
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Category.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
namespace Magento\Catalog\Model\ResourceModel;
1313

14+
use Magento\Catalog\Model\Indexer\Category\Product\Processor;
15+
use Magento\Framework\DataObject;
1416
use Magento\Framework\EntityManager\EntityManager;
1517

1618
/**
@@ -82,6 +84,11 @@ class Category extends AbstractResource
8284
*/
8385
protected $aggregateCount;
8486

87+
/**
88+
* @var Processor
89+
*/
90+
private $indexerProcessor;
91+
8592
/**
8693
* Category constructor.
8794
* @param \Magento\Eav\Model\Entity\Context $context
@@ -90,6 +97,7 @@ class Category extends AbstractResource
9097
* @param \Magento\Framework\Event\ManagerInterface $eventManager
9198
* @param Category\TreeFactory $categoryTreeFactory
9299
* @param Category\CollectionFactory $categoryCollectionFactory
100+
* @param Processor $indexerProcessor
93101
* @param array $data
94102
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
95103
*/
@@ -100,6 +108,7 @@ public function __construct(
100108
\Magento\Framework\Event\ManagerInterface $eventManager,
101109
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
102110
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
111+
Processor $indexerProcessor,
103112
$data = [],
104113
\Magento\Framework\Serialize\Serializer\Json $serializer = null
105114
) {
@@ -113,6 +122,7 @@ public function __construct(
113122
$this->_categoryCollectionFactory = $categoryCollectionFactory;
114123
$this->_eventManager = $eventManager;
115124
$this->connectionName = 'catalog';
125+
$this->indexerProcessor = $indexerProcessor;
116126
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
117127
->get(\Magento\Framework\Serialize\Serializer\Json::class);
118128
}
@@ -197,6 +207,19 @@ protected function _beforeDelete(\Magento\Framework\DataObject $object)
197207
$this->deleteChildren($object);
198208
}
199209

210+
/**
211+
* Mark Category indexer as invalid to be picked up by cron.
212+
*
213+
* @param DataObject $object
214+
* @return $this
215+
*/
216+
protected function _afterDelete(DataObject $object)
217+
{
218+
$this->indexerProcessor->markIndexerAsInvalid();
219+
return parent::_afterDelete($object);
220+
}
221+
222+
200223
/**
201224
* Delete children categories of specific category
202225
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Model\Plugin;
9+
10+
use Magento\Catalog\Model\ResourceModel\Category as Resource;
11+
use Magento\CatalogSearch\Model\Indexer\Fulltext\Processor;
12+
13+
/**
14+
* Perform indexer invalidation after a category delete.
15+
*/
16+
class Category
17+
{
18+
/**
19+
* @var Processor
20+
*/
21+
private $fulltextIndexerProcessor;
22+
23+
/**
24+
* @param Processor $fulltextIndexerProcessor
25+
*/
26+
public function __construct(Processor $fulltextIndexerProcessor)
27+
{
28+
$this->fulltextIndexerProcessor = $fulltextIndexerProcessor;
29+
}
30+
31+
/**
32+
* Mark fulltext indexer as invalid post-deletion of category.
33+
*
34+
* @param Resource $subjectCategory
35+
* @param Resource $resultCategory
36+
* @return Resource
37+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
*/
39+
public function afterDelete(Resource $subjectCategory, Resource $resultCategory) : Resource
40+
{
41+
$this->fulltextIndexerProcessor->markIndexerAsInvalid();
42+
43+
return $resultCategory;
44+
}
45+
}

app/code/Magento/CatalogSearch/etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@
3131
<argument name="filter" xsi:type="object">Magento\CatalogSearch\Ui\DataProvider\Product\AddFulltextFilterToCollection</argument>
3232
</arguments>
3333
</type>
34+
<type name="Magento\Catalog\Model\ResourceModel\Category">
35+
<plugin name="fulltext_search_indexer" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Model\Plugin\Category"/>
36+
</type>
3437
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Catalog\Model\ResourceModel\Category">
10+
<plugin name="fulltext_search_indexer" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Model\Plugin\Category"/>
11+
</type>
12+
</config>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Catalog\Model\ResourceModel\Category">
10+
<plugin name="fulltext_search_indexer" type="Magento\CatalogSearch\Model\Indexer\Fulltext\Model\Plugin\Category"/>
11+
</type>
12+
</config>

0 commit comments

Comments
 (0)