Hi there! I was looking for a solution to create a relation from a
blockto an
array fieldof a
global.
I have a global that recieves a Google calendar embed link and extracts the different sources. For each source I have the option to select a color via
ColorPicker.
export const Calendar: GlobalConfig = {
slug: 'calendar',
fields: [
{
name: 'embedLink',
label: {
en: 'Google Calendar Embed Link',
de: 'Google Kalender Einbettungslink',
},
type: 'text',
hooks: { /* hook that fills the calendarSources array */ },
},
{
name: 'calendarSources',
type: 'array',
admin: {
isSortable: false,
readOnly: true,
},
fields: [
{
name: 'calendarId',
type: 'text',
admin: {
hidden: true,
},
},
ColorPicker,
],
},
],
}In my
blockthat I'm creating I would like to reference these sources and choose which one to display in this block. I'm looking for something like this:
export const CalendarBlock: Block = {
slug: 'calendarBlock',
interfaceName: 'CalendarBlock',
fields: [
{
name: 'sources',
type: 'relationship',
relationTo: 'calendar.calendarSources',
hasMany: true,
}
],
}I would like to reference the
calendarSourcesarray on the
global. Which obviously does not work since it's not referencing a collection.
Is something like this doable in a way that I'm missing?
Or would the correct approach be to create some sort of readonly
collectionwhich I'd fill, instead of the array in the
global?
Easiest solution will be to create separate collection. If not - you can use text field + custom UI component that fetches your global, and save id of array item (they have id-s inside them ).
Yeah I restructured the sources into their own collection, which i reference now normally. Was hoping there would be a trick to reference the array, since it has its own database table and IDs anyway 🙂 but alright
Star
Discord
online
Get dedicated engineering support directly from the Payload team.