fix: search plugin localized fields (#7292)
This commit is contained in:
@@ -23,8 +23,17 @@ export const syncWithSearch: SyncWithSearch = async (args) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof beforeSync === 'function') {
|
if (typeof beforeSync === 'function') {
|
||||||
|
let docToSyncWith = doc
|
||||||
|
if (payload.config?.localization) {
|
||||||
|
docToSyncWith = await payload.findByID({
|
||||||
|
id,
|
||||||
|
collection,
|
||||||
|
locale: 'all',
|
||||||
|
req,
|
||||||
|
})
|
||||||
|
}
|
||||||
dataToSave = await beforeSync({
|
dataToSave = await beforeSync({
|
||||||
originalDoc: doc,
|
originalDoc: docToSyncWith,
|
||||||
payload,
|
payload,
|
||||||
req,
|
req,
|
||||||
searchDoc: dataToSave,
|
searchDoc: dataToSave,
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ export const searchPlugin =
|
|||||||
|
|
||||||
if (collections) {
|
if (collections) {
|
||||||
const pluginConfig: SearchPluginConfig = {
|
const pluginConfig: SearchPluginConfig = {
|
||||||
...incomingPluginConfig,
|
// write any config defaults here
|
||||||
deleteDrafts: true,
|
deleteDrafts: true,
|
||||||
syncDrafts: false,
|
syncDrafts: false,
|
||||||
// write any config defaults here
|
...incomingPluginConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
// add afterChange and afterDelete hooks to every search-enabled collection
|
// add afterChange and afterDelete hooks to every search-enabled collection
|
||||||
|
|||||||
@@ -26,5 +26,10 @@ export const Posts: CollectionConfig = {
|
|||||||
label: 'Excerpt',
|
label: 'Excerpt',
|
||||||
type: 'text',
|
type: 'text',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
name: 'slug',
|
||||||
|
localized: true,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,10 +31,13 @@ export default buildConfigWithDefaults({
|
|||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
searchPlugin({
|
searchPlugin({
|
||||||
beforeSync: ({ originalDoc, searchDoc }) => ({
|
beforeSync: ({ originalDoc, searchDoc }) => {
|
||||||
...searchDoc,
|
return {
|
||||||
excerpt: originalDoc?.excerpt || 'This is a fallback excerpt',
|
...searchDoc,
|
||||||
}),
|
excerpt: originalDoc?.excerpt || 'This is a fallback excerpt',
|
||||||
|
slug: originalDoc.slug,
|
||||||
|
}
|
||||||
|
},
|
||||||
collections: ['pages', 'posts'],
|
collections: ['pages', 'posts'],
|
||||||
defaultPriorities: {
|
defaultPriorities: {
|
||||||
pages: 10,
|
pages: 10,
|
||||||
@@ -50,6 +53,12 @@ export default buildConfigWithDefaults({
|
|||||||
position: 'sidebar',
|
position: 'sidebar',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'slug',
|
||||||
|
required: false,
|
||||||
|
type: 'text',
|
||||||
|
localized: true,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -184,4 +184,43 @@ describe('@payloadcms/plugin-search', () => {
|
|||||||
|
|
||||||
expect(deletedResults).toHaveLength(0)
|
expect(deletedResults).toHaveLength(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should sync localized data', async () => {
|
||||||
|
const createdDoc = await payload.create({
|
||||||
|
collection: 'posts',
|
||||||
|
data: {
|
||||||
|
_status: 'draft',
|
||||||
|
title: 'test title',
|
||||||
|
slug: 'es',
|
||||||
|
},
|
||||||
|
locale: 'es',
|
||||||
|
})
|
||||||
|
|
||||||
|
await payload.update({
|
||||||
|
collection: 'posts',
|
||||||
|
id: createdDoc.id,
|
||||||
|
data: {
|
||||||
|
_status: 'published',
|
||||||
|
title: 'test title',
|
||||||
|
slug: 'en',
|
||||||
|
},
|
||||||
|
locale: 'en',
|
||||||
|
})
|
||||||
|
|
||||||
|
const syncedSearchData = await payload.find({
|
||||||
|
collection: 'search',
|
||||||
|
locale: 'es',
|
||||||
|
where: {
|
||||||
|
and: [
|
||||||
|
{
|
||||||
|
'doc.value': {
|
||||||
|
equals: createdDoc.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(syncedSearchData.docs[0].slug).toEqual('es')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ export interface Config {
|
|||||||
'payload-preferences': PayloadPreference;
|
'payload-preferences': PayloadPreference;
|
||||||
'payload-migrations': PayloadMigration;
|
'payload-migrations': PayloadMigration;
|
||||||
};
|
};
|
||||||
|
db: {
|
||||||
|
defaultIDType: string;
|
||||||
|
};
|
||||||
globals: {};
|
globals: {};
|
||||||
locale: 'en' | 'es' | 'de';
|
locale: 'en' | 'es' | 'de';
|
||||||
user: User & {
|
user: User & {
|
||||||
@@ -29,13 +32,16 @@ export interface UserAuthOperations {
|
|||||||
email: string;
|
email: string;
|
||||||
};
|
};
|
||||||
login: {
|
login: {
|
||||||
password: string;
|
|
||||||
email: string;
|
email: string;
|
||||||
|
password: string;
|
||||||
};
|
};
|
||||||
registerFirstUser: {
|
registerFirstUser: {
|
||||||
email: string;
|
email: string;
|
||||||
password: string;
|
password: string;
|
||||||
};
|
};
|
||||||
|
unlock: {
|
||||||
|
email: string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* This interface was referenced by `Config`'s JSON-Schema
|
* This interface was referenced by `Config`'s JSON-Schema
|
||||||
@@ -74,6 +80,7 @@ export interface Post {
|
|||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
excerpt?: string | null;
|
excerpt?: string | null;
|
||||||
|
slug?: string | null;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
_status?: ('draft' | 'published') | null;
|
_status?: ('draft' | 'published') | null;
|
||||||
@@ -96,6 +103,7 @@ export interface Search {
|
|||||||
value: string | Post;
|
value: string | Post;
|
||||||
};
|
};
|
||||||
excerpt?: string | null;
|
excerpt?: string | null;
|
||||||
|
slug?: string | null;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user