Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Relation to array field of global

default discord avatar
zammy25033 months ago
2

Hi there! I was looking for a solution to create a relation from a

block

to an

array field

of 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

block

that 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

calendarSources

array 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

collection

which I'd fill, instead of the array in the

global

?

  • default discord avatar
    veiag3 months ago

    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 ).

  • default discord avatar
    zammy25033 months ago

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.