File tree Expand file tree Collapse file tree 6 files changed +78
-8
lines changed
packages/plugin-search/src Expand file tree Collapse file tree 6 files changed +78
-8
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,17 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
23
23
}
24
24
25
25
if ( typeof beforeSync === 'function' ) {
26
+ let docToSyncWith = doc
27
+ if ( payload . config ?. localization ) {
28
+ docToSyncWith = await payload . findByID ( {
29
+ id,
30
+ collection,
31
+ locale : 'all' ,
32
+ req,
33
+ } )
34
+ }
26
35
dataToSave = await beforeSync ( {
27
- originalDoc : doc ,
36
+ originalDoc : docToSyncWith ,
28
37
payload,
29
38
req,
30
39
searchDoc : dataToSave ,
Original file line number Diff line number Diff line change @@ -13,10 +13,10 @@ export const searchPlugin =
13
13
14
14
if ( collections ) {
15
15
const pluginConfig : SearchPluginConfig = {
16
- ... incomingPluginConfig ,
16
+ // write any config defaults here
17
17
deleteDrafts : true ,
18
18
syncDrafts : false ,
19
- // write any config defaults here
19
+ ... incomingPluginConfig ,
20
20
}
21
21
22
22
// add afterChange and afterDelete hooks to every search-enabled collection
Original file line number Diff line number Diff line change @@ -26,5 +26,10 @@ export const Posts: CollectionConfig = {
26
26
label : 'Excerpt' ,
27
27
type : 'text' ,
28
28
} ,
29
+ {
30
+ type : 'text' ,
31
+ name : 'slug' ,
32
+ localized : true ,
33
+ } ,
29
34
] ,
30
35
}
Original file line number Diff line number Diff line change @@ -31,10 +31,13 @@ export default buildConfigWithDefaults({
31
31
} ,
32
32
plugins : [
33
33
searchPlugin ( {
34
- beforeSync : ( { originalDoc, searchDoc } ) => ( {
35
- ...searchDoc ,
36
- excerpt : originalDoc ?. excerpt || 'This is a fallback excerpt' ,
37
- } ) ,
34
+ beforeSync : ( { originalDoc, searchDoc } ) => {
35
+ return {
36
+ ...searchDoc ,
37
+ excerpt : originalDoc ?. excerpt || 'This is a fallback excerpt' ,
38
+ slug : originalDoc . slug ,
39
+ }
40
+ } ,
38
41
collections : [ 'pages' , 'posts' ] ,
39
42
defaultPriorities : {
40
43
pages : 10 ,
@@ -50,6 +53,12 @@ export default buildConfigWithDefaults({
50
53
position : 'sidebar' ,
51
54
} ,
52
55
} ,
56
+ {
57
+ name : 'slug' ,
58
+ required : false ,
59
+ type : 'text' ,
60
+ localized : true ,
61
+ } ,
53
62
] ,
54
63
} ,
55
64
} ) ,
Original file line number Diff line number Diff line change @@ -184,4 +184,43 @@ describe('@payloadcms/plugin-search', () => {
184
184
185
185
expect ( deletedResults ) . toHaveLength ( 0 )
186
186
} )
187
+
188
+ it ( 'should sync localized data' , async ( ) => {
189
+ const createdDoc = await payload . create ( {
190
+ collection : 'posts' ,
191
+ data : {
192
+ _status : 'draft' ,
193
+ title : 'test title' ,
194
+ slug : 'es' ,
195
+ } ,
196
+ locale : 'es' ,
197
+ } )
198
+
199
+ await payload . update ( {
200
+ collection : 'posts' ,
201
+ id : createdDoc . id ,
202
+ data : {
203
+ _status : 'published' ,
204
+ title : 'test title' ,
205
+ slug : 'en' ,
206
+ } ,
207
+ locale : 'en' ,
208
+ } )
209
+
210
+ const syncedSearchData = await payload . find ( {
211
+ collection : 'search' ,
212
+ locale : 'es' ,
213
+ where : {
214
+ and : [
215
+ {
216
+ 'doc.value' : {
217
+ equals : createdDoc . id ,
218
+ } ,
219
+ } ,
220
+ ] ,
221
+ } ,
222
+ } )
223
+
224
+ expect ( syncedSearchData . docs [ 0 ] . slug ) . toEqual ( 'es' )
225
+ } )
187
226
} )
Original file line number Diff line number Diff line change @@ -18,6 +18,9 @@ export interface Config {
18
18
'payload-preferences' : PayloadPreference ;
19
19
'payload-migrations' : PayloadMigration ;
20
20
} ;
21
+ db : {
22
+ defaultIDType : string ;
23
+ } ;
21
24
globals : { } ;
22
25
locale : 'en' | 'es' | 'de' ;
23
26
user : User & {
@@ -29,13 +32,16 @@ export interface UserAuthOperations {
29
32
email : string ;
30
33
} ;
31
34
login : {
32
- password : string ;
33
35
email : string ;
36
+ password : string ;
34
37
} ;
35
38
registerFirstUser : {
36
39
email : string ;
37
40
password : string ;
38
41
} ;
42
+ unlock : {
43
+ email : string ;
44
+ } ;
39
45
}
40
46
/**
41
47
* This interface was referenced by `Config`'s JSON-Schema
@@ -74,6 +80,7 @@ export interface Post {
74
80
id : string ;
75
81
title : string ;
76
82
excerpt ?: string | null ;
83
+ slug ?: string | null ;
77
84
updatedAt : string ;
78
85
createdAt : string ;
79
86
_status ?: ( 'draft' | 'published' ) | null ;
@@ -96,6 +103,7 @@ export interface Search {
96
103
value : string | Post ;
97
104
} ;
98
105
excerpt ?: string | null ;
106
+ slug ?: string | null ;
99
107
updatedAt : string ;
100
108
createdAt : string ;
101
109
}
You can’t perform that action at this time.
0 commit comments