Skip to content

Commit 405c563

Browse files
committed
integrate publishAllLocales in create operation
1 parent a7cef11 commit 405c563

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

packages/payload/src/collections/endpoints/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { createOperation } from '../operations/create.js'
1111
export const createHandler: PayloadHandler = async (req) => {
1212
const collection = getRequestCollection(req)
1313

14-
const { autosave, depth, draft, populate, select } = parseParams(req.query)
14+
const { autosave, depth, draft, populate, publishAllLocales, select } = parseParams(req.query)
1515

1616
const publishSpecificLocale = req.query.publishSpecificLocale as string | undefined
1717

@@ -22,6 +22,7 @@ export const createHandler: PayloadHandler = async (req) => {
2222
depth,
2323
draft,
2424
populate,
25+
publishAllLocales,
2526
publishSpecificLocale,
2627
req,
2728
select,

packages/payload/src/collections/operations/create.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export type Arguments<TSlug extends CollectionSlug> = {
4747
overrideAccess?: boolean
4848
overwriteExistingFiles?: boolean
4949
populate?: PopulateType
50+
publishAllLocales?: boolean
5051
publishSpecificLocale?: string
5152
req: PayloadRequest
5253
select?: SelectType
@@ -105,6 +106,7 @@ export const createOperation = async <
105106
overrideAccess,
106107
overwriteExistingFiles = false,
107108
populate,
109+
publishAllLocales,
108110
publishSpecificLocale,
109111
req: {
110112
fallbackLocale,
@@ -120,7 +122,11 @@ export const createOperation = async <
120122

121123
let { data } = args
122124

123-
const isSavingDraft = Boolean(draft && collectionConfig.versions.drafts)
125+
const isSavingDraft = Boolean(draft && collectionConfig.versions.drafts && !publishAllLocales)
126+
127+
if (isSavingDraft) {
128+
data._status = 'draft'
129+
}
124130

125131
let duplicatedFromDocWithLocales: JsonObject = {}
126132
let duplicatedFromDoc: JsonObject = {}
@@ -237,6 +243,34 @@ export const createOperation = async <
237243
!collectionConfig.versions.drafts.validate,
238244
})
239245

246+
if (
247+
config.localization &&
248+
collectionConfig.versions &&
249+
collectionConfig.versions.drafts &&
250+
collectionConfig.versions.drafts.localizeStatus &&
251+
publishAllLocales
252+
) {
253+
let accessibleLocaleCodes = config.localization.localeCodes
254+
255+
if (config.localization.filterAvailableLocales) {
256+
const filteredLocales = await config.localization.filterAvailableLocales({
257+
locales: config.localization.locales,
258+
req,
259+
})
260+
accessibleLocaleCodes = filteredLocales.map((locale) =>
261+
typeof locale === 'string' ? locale : locale.code,
262+
)
263+
}
264+
265+
if (typeof resultWithLocales._status !== 'object' || resultWithLocales._status === null) {
266+
resultWithLocales._status = {}
267+
}
268+
269+
for (const localeCode of accessibleLocaleCodes) {
270+
resultWithLocales._status[localeCode] = 'published'
271+
}
272+
}
273+
240274
// /////////////////////////////////////
241275
// Write files to local storage
242276
// /////////////////////////////////////

packages/payload/src/collections/operations/local/create.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ type BaseOptions<TSlug extends CollectionSlug, TSelect extends SelectType> = {
8888
* Specify [populate](https://payloadcms.com/docs/queries/select#populate) to control which fields to include to the result from populated documents.
8989
*/
9090
populate?: PopulateType
91+
/**
92+
* Publish to all locales
93+
*/
94+
publishAllLocales?: boolean
9195
/**
9296
* The `PayloadRequest` object. You can pass it to thread the current [transaction](https://payloadcms.com/docs/database/transactions), user and locale to the operation.
9397
* Recommended to pass when using the Local API from hooks, as usually you want to execute the operation within the current transaction.
@@ -151,6 +155,7 @@ export async function createLocal<
151155
overrideAccess = true,
152156
overwriteExistingFiles = false,
153157
populate,
158+
publishAllLocales,
154159
select,
155160
showHiddenFields,
156161
} = options
@@ -178,6 +183,7 @@ export async function createLocal<
178183
overrideAccess,
179184
overwriteExistingFiles,
180185
populate,
186+
publishAllLocales,
181187
req,
182188
select,
183189
showHiddenFields,

test/localization/int.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,6 +3639,26 @@ describe('Localization', () => {
36393639
expect(esDoc._status).toContain('draft')
36403640
})
36413641

3642+
it('should allow publishing of all locales upon creation', async () => {
3643+
const doc = await payload.create({
3644+
collection: allFieldsLocalizedSlug,
3645+
data: {
3646+
text: 'Localized Metadata EN',
3647+
_status: 'published',
3648+
},
3649+
locale: defaultLocale,
3650+
publishAllLocales: true,
3651+
})
3652+
3653+
const esDoc = await payload.findByID({
3654+
locale: spanishLocale,
3655+
id: doc.id,
3656+
collection: allFieldsLocalizedSlug,
3657+
})
3658+
3659+
expect(esDoc._status).toContain('published')
3660+
})
3661+
36423662
it('should return correct data based on draft arg', async () => {
36433663
// NOTE: passes in MongoDB, fails in PG
36443664
// -> fails to query on version._status.[localeCode] in `replaceWithDraftIfAvailable` when locale = 'all'

0 commit comments

Comments
 (0)