Skip to content

Commit f4d526d

Browse files
authored
fix: fallbackLocale not respecting default settings, locale specific fallbacks and not respecting 'none' or false (#8591)
This PR fixes and improves a few things around localisation and fallbackLocale: - For the REST API `fallbackLocale` and `fallback-locale` are treated the same for consistency with the Local API - `fallback: false` in config is now respected, by default results will not fallback to `defaultLocale` unless this config is true, can also be overridden by providing an explicit `fallbackLocale` in the request - locale specific fallbacks will now take priority over `defaultLocale` unless an explicit fallback is provided - Fixes types on operations to allow `'none'` as a value for fallbackLocale - `fallback` is now true by default if unspecified Closes #8443
1 parent 3b55458 commit f4d526d

File tree

43 files changed

+2234
-1980
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2234
-1980
lines changed

docs/configuration/localization.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default buildConfig({
6565
},
6666
],
6767
defaultLocale: 'en', // required
68-
fallback: true,
68+
fallback: true, // defaults to true
6969
},
7070
})
7171
```
@@ -81,7 +81,7 @@ The following options are available:
8181
| -------------- | ------------------------------------------------------------------------------------------------------------------------------ |
8282
| **`locales`** | Array of all the languages that you would like to support. [More details](#locales) |
8383
| **`defaultLocale`** | Required string that matches one of the locale codes from the array provided. By default, if no locale is specified, documents will be returned in this locale. |
84-
| **`fallback`** | Boolean enabling "fallback" locale functionality. If a document is requested in a locale, but a field does not have a localized value corresponding to the requested locale, then if this property is enabled, the document will automatically fall back to the fallback locale value. If this property is not enabled, the value will not be populated. |
84+
| **`fallback`** | Boolean enabling "fallback" locale functionality. If a document is requested in a locale, but a field does not have a localized value corresponding to the requested locale, then if this property is enabled, the document will automatically fall back to the fallback locale value. If this property is not enabled, the value will not be populated unless a fallback is explicitly provided in the request. True by default. |
8585

8686
### Locales
8787

examples/multi-tenant/src/app/components/Login/client.page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export const Login = ({ tenantSlug }: Props) => {
2020

2121
const handleSubmit = async (e: FormEvent) => {
2222
e.preventDefault()
23-
if (!usernameRef?.current?.value || !passwordRef?.current?.value) {return}
23+
if (!usernameRef?.current?.value || !passwordRef?.current?.value) {
24+
return
25+
}
2426
const actionRes = await fetch(
2527
`${process.env.NEXT_PUBLIC_SERVER_URL}/api/users/external-users/login`,
2628
{

examples/multi-tenant/src/collections/Pages/hooks/ensureUniqueSlug.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { getTenantAccessIDs } from '../../../utilities/getTenantAccessIDs'
66

77
export const ensureUniqueSlug: FieldHook = async ({ data, originalDoc, req, value }) => {
88
// if value is unchanged, skip validation
9-
if (originalDoc.slug === value) {return value}
9+
if (originalDoc.slug === value) {
10+
return value
11+
}
1012

1113
const incomingTenantID = typeof data?.tenant === 'object' ? data.tenant.id : data?.tenant
1214
const currentTenantID =

examples/multi-tenant/src/collections/Pages/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export const Pages: CollectionConfig = {
1414
read: (args) => {
1515
// when viewing pages inside the admin panel
1616
// restrict access to the ones your user has access to
17-
if (isPayloadAdminPanel(args.req)) {return filterByTenantRead(args)}
17+
if (isPayloadAdminPanel(args.req)) {
18+
return filterByTenantRead(args)
19+
}
1820

1921
// when viewing pages from outside the admin panel
2022
// you should be able to see your tenants and public tenants

examples/multi-tenant/src/collections/Tenants/access/read.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export const tenantRead: Access = (args) => {
77
const req = args.req
88

99
// Super admin can read all
10-
if (isSuperAdmin(args)) {return true}
10+
if (isSuperAdmin(args)) {
11+
return true
12+
}
1113

1214
const tenantIDs = getTenantAccessIDs(req.user)
1315

examples/multi-tenant/src/collections/Users/access/create.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ import { getTenantAdminTenantAccessIDs } from '../../../utilities/getTenantAcces
77

88
export const createAccess: Access<User> = (args) => {
99
const { req } = args
10-
if (!req.user) {return false}
10+
if (!req.user) {
11+
return false
12+
}
1113

12-
if (isSuperAdmin(args)) {return true}
14+
if (isSuperAdmin(args)) {
15+
return true
16+
}
1317

1418
const adminTenantAccessIDs = getTenantAdminTenantAccessIDs(req.user)
1519

16-
if (adminTenantAccessIDs.length > 0) {return true}
20+
if (adminTenantAccessIDs.length > 0) {
21+
return true
22+
}
1723

1824
return false
1925
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Access } from 'payload'
22

33
export const isAccessingSelf: Access = ({ id, req }) => {
4-
if (!req?.user) {return false}
4+
if (!req?.user) {
5+
return false
6+
}
57
return req.user.id === id
68
}

examples/multi-tenant/src/collections/Users/access/updateAndDelete.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { getTenantAdminTenantAccessIDs } from '../../../utilities/getTenantAcces
55

66
export const updateAndDeleteAccess: Access = (args) => {
77
const { req } = args
8-
if (!req.user) {return false}
8+
if (!req.user) {
9+
return false
10+
}
911

10-
if (isSuperAdmin(args)) {return true}
12+
if (isSuperAdmin(args)) {
13+
return true
14+
}
1115

1216
const adminTenantAccessIDs = getTenantAdminTenantAccessIDs(req.user)
1317

examples/multi-tenant/src/collections/Users/hooks/ensureUniqueUsername.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { getTenantAccessIDs } from '../../../utilities/getTenantAccessIDs'
66

77
export const ensureUniqueUsername: FieldHook = async ({ data, originalDoc, req, value }) => {
88
// if value is unchanged, skip validation
9-
if (originalDoc.username === value) {return value}
9+
if (originalDoc.username === value) {
10+
return value
11+
}
1012

1113
const incomingTenantID = typeof data?.tenant === 'object' ? data.tenant.id : data?.tenant
1214
const currentTenantID =

examples/multi-tenant/src/fields/TenantField/hooks/autofillTenant.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export const autofillTenant: FieldHook = ({ req, value }) => {
88
// return that tenant ID as the value
99
if (!value) {
1010
const tenantIDs = getTenantAccessIDs(req.user)
11-
if (tenantIDs.length === 1) {return tenantIDs[0]}
11+
if (tenantIDs.length === 1) {
12+
return tenantIDs[0]
13+
}
1214
}
1315

1416
return value

0 commit comments

Comments
 (0)