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.

Use globals array as select options in field

default discord avatar
losolio7 months ago
1

Is it possible to lookup options from globals, and use them as options in a select field? Tried this in payload v3.12, but does not seem to work:



import { GlobalConfig } from 'payload';

export const LinkCategoriesGlobal: GlobalConfig = {
  slug: 'link-categories',
  fields: [
    {
      name: 'linkCategories',
      type: 'array',
      fields: [
        {
          name: 'name',
          type: 'text',
          required: true,
        },
      ],
    },
  ],
};


and then in the field definition something like this?


import type { Field } from 'payload';

export const relatedLinks: Field = {
  name: 'related-links',
  type: 'array',
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'linkCategory',
          label: 'Category',
          type: 'select',
          required: false,
          options: async ({ payload }) => {
            const globalData = await payload.findGlobal({ slug: 'link-categories' });
            return globalData.linkCategories.map(({ name, id }) => ({
              label: name,
              value: id,
            }));
          },
          admin: {
            description: 'Select a link category.',
            placeholder: 'Select a category...',
          },
        },
        {
          name: 'url',
          label: 'URL',
          type: 'text',
          required: true,
          admin: {
            description: 'The URL for the link.',
          },
        },
      ],
    },
  ],
};


Thanks for the links, bot... Found the title of this video, which solved my issue

https://www.youtube.com/watch?v=Efn9OxSjA6Y
    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.