import { getPayload } from 'payload' import configPromise from '../src/payload.config' async function run() { const payload = await getPayload({ config: configPromise }) const servicesData: Record = { pl: [], de: [] } for (const locale of ['pl', 'de']) { const pages = await payload.find({ collection: 'pages', locale: locale as 'pl' | 'de', where: { slug: { equals: locale === 'de' ? 'leistungen' : 'uslugi' } } }) if (pages.docs.length > 0) { const dbPage = pages.docs[0] const servicesBlock = dbPage.layout?.find((b: any) => b.blockType === 'servicesDetailed') as any if (servicesBlock && servicesBlock.services) { servicesData[locale] = servicesBlock.services.map((s: any) => ({ label: s.title, value: s.anchor || s.title.toLowerCase().replace(/[^a-z0-9]+/g, '-') })) } } } const baseValues = servicesData['pl'].map(s => s.value); // First pass: PL. This will create the options (or update them). let form = await payload.findByID({ collection: 'forms', id: 1, locale: 'pl', }) let updatedFieldsPL = form.fields?.map((f: any) => { if (f.name === 'name') { f.defaultValue = 'Jan Kowalski' f.label = 'Imię i nazwisko' } if (f.name === 'email') { f.defaultValue = 'jan@przyklad.pl' f.label = 'Adres e-mail' } if (f.name === 'message') { f.defaultValue = 'Opisz krótko swoją sytuację...' f.label = 'Wiadomość' } if (f.name === 'consent') { f.label = 'Wyrażam zgodę na przetwarzanie danych' } if (f.name === 'service' && f.blockType === 'select') { f.label = 'Usługa' f.options = servicesData['pl'].map((s: any, idx: number) => { // preserve existing id if we have the exact same amount of options const existingOption = f.options?.[idx]; return { id: existingOption ? existingOption.id : undefined, label: s.label, value: baseValues[idx] } }) } return f }) await payload.update({ collection: 'forms', id: 1, locale: 'pl', data: { fields: updatedFieldsPL } }) console.log(`Updated form 1 for locale pl`) // Refetch the form to get the assigned IDs of the options! form = await payload.findByID({ collection: 'forms', id: 1, locale: 'all', // Get all locales to see the IDs }) let updatedFieldsDE = form.fields?.map((f: any) => { if (f.name === 'name') { f.defaultValue = 'Max Mustermann' f.label = 'Vor- und Nachname' } if (f.name === 'email') { f.defaultValue = 'max@beispiel.de' f.label = 'E-Mail Adresse' } if (f.name === 'message') { f.defaultValue = 'Beschreiben Sie kurz Ihre Situation...' f.label = 'Nachricht' } if (f.name === 'consent') { f.label = 'Ich stimme der Datenverarbeitung zu' } if (f.name === 'service' && f.blockType === 'select') { f.label = 'Dienstleistung' f.options = servicesData['de'].map((s: any, idx: number) => { // VERY IMPORTANT: Use the ID assigned during the PL update const optionId = f.options?.[idx]?.id; return { id: optionId, label: s.label, value: baseValues[idx] } }) } return f }) await payload.update({ collection: 'forms', id: 1, locale: 'de', data: { fields: updatedFieldsDE } }) console.log(`Updated form 1 for locale de`) process.exit(0) } run().catch(console.error)