Skip to content

Commit 5aa3283

Browse files
fix: search plugin localized fields (#7292)
1 parent 4584478 commit 5aa3283

File tree

6 files changed

+78
-8
lines changed

6 files changed

+78
-8
lines changed

packages/plugin-search/src/Search/hooks/syncWithSearch.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,17 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
2323
}
2424

2525
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+
}
2635
dataToSave = await beforeSync({
27-
originalDoc: doc,
36+
originalDoc: docToSyncWith,
2837
payload,
2938
req,
3039
searchDoc: dataToSave,

packages/plugin-search/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export const searchPlugin =
1313

1414
if (collections) {
1515
const pluginConfig: SearchPluginConfig = {
16-
...incomingPluginConfig,
16+
// write any config defaults here
1717
deleteDrafts: true,
1818
syncDrafts: false,
19-
// write any config defaults here
19+
...incomingPluginConfig,
2020
}
2121

2222
// add afterChange and afterDelete hooks to every search-enabled collection

test/plugin-search/collections/Posts.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ export const Posts: CollectionConfig = {
2626
label: 'Excerpt',
2727
type: 'text',
2828
},
29+
{
30+
type: 'text',
31+
name: 'slug',
32+
localized: true,
33+
},
2934
],
3035
}

test/plugin-search/config.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ export default buildConfigWithDefaults({
3131
},
3232
plugins: [
3333
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+
},
3841
collections: ['pages', 'posts'],
3942
defaultPriorities: {
4043
pages: 10,
@@ -50,6 +53,12 @@ export default buildConfigWithDefaults({
5053
position: 'sidebar',
5154
},
5255
},
56+
{
57+
name: 'slug',
58+
required: false,
59+
type: 'text',
60+
localized: true,
61+
},
5362
],
5463
},
5564
}),

test/plugin-search/int.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,43 @@ describe('@payloadcms/plugin-search', () => {
184184

185185
expect(deletedResults).toHaveLength(0)
186186
})
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+
})
187226
})

test/plugin-search/payload-types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export interface Config {
1818
'payload-preferences': PayloadPreference;
1919
'payload-migrations': PayloadMigration;
2020
};
21+
db: {
22+
defaultIDType: string;
23+
};
2124
globals: {};
2225
locale: 'en' | 'es' | 'de';
2326
user: User & {
@@ -29,13 +32,16 @@ export interface UserAuthOperations {
2932
email: string;
3033
};
3134
login: {
32-
password: string;
3335
email: string;
36+
password: string;
3437
};
3538
registerFirstUser: {
3639
email: string;
3740
password: string;
3841
};
42+
unlock: {
43+
email: string;
44+
};
3945
}
4046
/**
4147
* This interface was referenced by `Config`'s JSON-Schema
@@ -74,6 +80,7 @@ export interface Post {
7480
id: string;
7581
title: string;
7682
excerpt?: string | null;
83+
slug?: string | null;
7784
updatedAt: string;
7885
createdAt: string;
7986
_status?: ('draft' | 'published') | null;
@@ -96,6 +103,7 @@ export interface Search {
96103
value: string | Post;
97104
};
98105
excerpt?: string | null;
106+
slug?: string | null;
99107
updatedAt: string;
100108
createdAt: string;
101109
}

0 commit comments

Comments
 (0)