6
6
namespace Magento \CatalogSearch \Model \Indexer \Fulltext \Plugin ;
7
7
8
8
use Magento \CatalogSearch \Model \Indexer \Fulltext ;
9
+ use Magento \Framework \Model \AbstractModel ;
10
+ use Magento \Catalog \Model \ResourceModel \Attribute as AttributeResourceModel ;
11
+ use Magento \Framework \Search \Request \Config ;
12
+ use Magento \Framework \Indexer \IndexerRegistry ;
13
+ use Magento \Catalog \Api \Data \EavAttributeInterface ;
9
14
10
15
/**
11
16
* Catalog search indexer plugin for catalog attribute.
12
17
*/
13
18
class Attribute extends AbstractPlugin
14
19
{
15
20
/**
16
- * @var \Magento\Framework\Search\Request\ Config
21
+ * @var Config
17
22
*/
18
23
private $ config ;
19
24
@@ -33,12 +38,12 @@ class Attribute extends AbstractPlugin
33
38
private $ saveIsNew ;
34
39
35
40
/**
36
- * @param \Magento\Framework\Indexer\ IndexerRegistry $indexerRegistry
37
- * @param \Magento\Framework\Search\Request\ Config $config
41
+ * @param IndexerRegistry $indexerRegistry
42
+ * @param Config $config
38
43
*/
39
44
public function __construct (
40
- \ Magento \ Framework \ Indexer \ IndexerRegistry $ indexerRegistry ,
41
- \ Magento \ Framework \ Search \ Request \ Config $ config
45
+ IndexerRegistry $ indexerRegistry ,
46
+ Config $ config
42
47
) {
43
48
parent ::__construct ($ indexerRegistry );
44
49
$ this ->config = $ config ;
@@ -47,36 +52,32 @@ public function __construct(
47
52
/**
48
53
* Check if indexer invalidation is needed on attribute save (searchable flag change)
49
54
*
50
- * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject
51
- * @param \Magento\Framework\Model\ AbstractModel $attribute
55
+ * @param AttributeResourceModel $subject
56
+ * @param AbstractModel $attribute
52
57
*
53
58
* @return void
54
59
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
55
60
*/
56
61
public function beforeSave (
57
- \ Magento \ Catalog \ Model \ ResourceModel \ Attribute $ subject ,
58
- \ Magento \ Framework \ Model \ AbstractModel $ attribute
62
+ AttributeResourceModel $ subject ,
63
+ AbstractModel $ attribute
59
64
) {
60
65
$ this ->saveIsNew = $ attribute ->isObjectNew ();
61
- $ this ->saveNeedInvalidation = (
62
- $ attribute ->dataHasChangedFor ('is_searchable ' )
63
- || $ attribute ->dataHasChangedFor ('is_filterable ' )
64
- || $ attribute ->dataHasChangedFor ('is_visible_in_advanced_search ' )
65
- );
66
+ $ this ->saveNeedInvalidation = $ this ->shouldInvalidateSearchIndex ($ attribute );
66
67
}
67
68
68
69
/**
69
70
* Invalidate indexer on attribute save (searchable flag change)
70
71
*
71
- * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject
72
- * @param \Magento\Catalog\Model\ResourceModel\Attribute $result
72
+ * @param AttributeResourceModel $subject
73
+ * @param AttributeResourceModel $result
73
74
*
74
- * @return \Magento\Catalog\Model\ResourceModel\Attribute
75
+ * @return AttributeResourceModel
75
76
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
76
77
*/
77
78
public function afterSave (
78
- \ Magento \ Catalog \ Model \ ResourceModel \ Attribute $ subject ,
79
- \ Magento \ Catalog \ Model \ ResourceModel \ Attribute $ result
79
+ AttributeResourceModel $ subject ,
80
+ AttributeResourceModel $ result
80
81
) {
81
82
if ($ this ->saveNeedInvalidation ) {
82
83
$ this ->indexerRegistry ->get (Fulltext::INDEXER_ID )->invalidate ();
@@ -91,35 +92,61 @@ public function afterSave(
91
92
/**
92
93
* Check if indexer invalidation is needed on searchable attribute delete
93
94
*
94
- * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject
95
- * @param \Magento\Framework\Model\ AbstractModel $attribute
95
+ * @param AttributeResourceModel $subject
96
+ * @param AbstractModel $attribute
96
97
*
97
98
* @return void
98
99
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
99
100
*/
100
101
public function beforeDelete (
101
- \ Magento \ Catalog \ Model \ ResourceModel \ Attribute $ subject ,
102
- \ Magento \ Framework \ Model \ AbstractModel $ attribute
102
+ AttributeResourceModel $ subject ,
103
+ AbstractModel $ attribute
103
104
) {
104
105
$ this ->deleteNeedInvalidation = !$ attribute ->isObjectNew () && $ attribute ->getIsSearchable ();
105
106
}
106
107
107
108
/**
108
109
* Invalidate indexer on searchable attribute delete
109
110
*
110
- * @param \Magento\Catalog\Model\ResourceModel\Attribute $subject
111
- * @param \Magento\Catalog\Model\ResourceModel\Attribute $result
111
+ * @param AttributeResourceModel $subject
112
+ * @param AttributeResourceModel $result
112
113
*
113
- * @return \Magento\Catalog\Model\ResourceModel\Attribute
114
+ * @return AttributeResourceModel
114
115
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
115
116
*/
116
117
public function afterDelete (
117
- \ Magento \ Catalog \ Model \ ResourceModel \ Attribute $ subject ,
118
- \ Magento \ Catalog \ Model \ ResourceModel \ Attribute $ result
118
+ AttributeResourceModel $ subject ,
119
+ AttributeResourceModel $ result
119
120
) {
120
121
if ($ this ->deleteNeedInvalidation ) {
121
122
$ this ->indexerRegistry ->get (Fulltext::INDEXER_ID )->invalidate ();
122
123
}
123
124
return $ result ;
124
125
}
126
+
127
+ /**
128
+ * Check if catalogsearch_fulltext index should be invalidated.
129
+ *
130
+ * @param AbstractModel $attribute
131
+ * @return bool
132
+ */
133
+ private function shouldInvalidateSearchIndex (
134
+ AbstractModel $ attribute
135
+ ):bool {
136
+ $ shouldInvalidate = false ;
137
+ $ fields = [
138
+ EavAttributeInterface::IS_SEARCHABLE ,
139
+ EavAttributeInterface::IS_FILTERABLE ,
140
+ EavAttributeInterface::IS_VISIBLE_IN_ADVANCED_SEARCH ,
141
+ ];
142
+ foreach ($ fields as $ field ) {
143
+ if ($ this ->saveIsNew && $ attribute ->getData ($ field )
144
+ || !$ this ->saveIsNew && $ attribute ->dataHasChangedFor ($ field )) {
145
+ $ shouldInvalidate = true ;
146
+ break ;
147
+ }
148
+ }
149
+
150
+ return $ shouldInvalidate ;
151
+ }
125
152
}
0 commit comments