Skip to content

Commit a63a3d0

Browse files
feat(ui): adds filtering config option and implementation for filtering a… (#11007)
Adds the ability to filter what locales should be available per request. This means that you can determine what locales are visible in the localizer selection menu at the top of the admin panel. You could do this per user, or implement a function that scopes these to tenants and more. Here is an example function that would scope certain locales to tenants: **`payload.config.ts`** ```ts // ... rest of payload config localization: { defaultLocale: 'en', locales: ['en', 'es'], filterAvailableLocales: async ({ req, locales }) => { if (getTenantFromCookie(req.headers, 'text')) { try { const fullTenant = await req.payload.findByID({ id: getTenantFromCookie(req.headers, 'text') as string, collection: 'tenants', }) if (fullTenant && fullTenant.supportedLocales?.length) { return locales.filter((locale) => { return fullTenant.supportedLocales?.includes(locale.code as 'en' | 'es') }) } } catch (_) { // do nothing } } return locales }, } ``` The filter above assumes you have a field on your tenants collection like so: ```ts { name: 'supportedLocales', type: 'select', hasMany: true, options: [ { label: 'English', value: 'en', }, { label: 'Spanish', value: 'es', }, ], } ```
1 parent 57143b3 commit a63a3d0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/next/src/layouts/Root/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ export const RootLayout = async ({
9191
importMap,
9292
})
9393

94+
if (
95+
clientConfig.localization &&
96+
config.localization &&
97+
typeof config.localization.filterAvailableLocales === 'function'
98+
) {
99+
clientConfig.localization.locales = (
100+
await config.localization.filterAvailableLocales({
101+
locales: config.localization.locales,
102+
req,
103+
})
104+
).map(({ toString, ...rest }) => rest)
105+
clientConfig.localization.localeCodes = config.localization.locales.map(({ code }) => code)
106+
}
107+
94108
const locale = await getRequestLocale({
95109
req,
96110
})

packages/payload/src/config/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ export type BaseLocalizationConfig = {
470470
* @default true
471471
*/
472472
fallback?: boolean
473+
/**
474+
* Define a function to filter the locales made available in Payload admin UI
475+
* based on user.
476+
*/
477+
filterAvailableLocales?: (args: {
478+
locales: Locale[]
479+
req: PayloadRequest
480+
}) => Locale[] | Promise<Locale[]>
473481
}
474482

475483
export type LocalizationConfigWithNoLabels = Prettify<

0 commit comments

Comments
 (0)