Preview button not showing on all collections (?)

default discord avatar
adam-mrozik
11 months ago
1 2

Hey,

I've added preview function to 8 different collections, but for some reason it only appears on a few of them. Are there any configurations that could prevent such a button from being shown? Not really sure if it is a bug, or me missing something crucial

Example non-working code (same preview function is used in the working code as well):

export const Plans: CollectionConfig = {
  slug: 'plans',
  admin: {
    useAsTitle: 'title',
    preview: (doc, { locale }) => {
            if (doc?.slug) {
                return `https://bigbird.com/preview/posts/${doc.slug}?locale=${locale}`;
            }

            return null;
        },
    },
  },
  access: DEFAULT_ACCESS_CONTROL,
  fields: [
    {
      name: 'title',
      label: 'Title',
      type: 'text',
      localized: true,
    },
    {
      name: 'id',
      label: 'Custom ID',
      type: 'text',
    },
    buttons,
  ],
  timestamps: false,
}
  • default discord avatar
    evelynhathaway
    9 months ago

    In your above code, if (doc?.slug) makes the preview button only show if there's a slug.

    You can write another condition or remove the condition and just return the preview URL.

    As an example:

    export const Plans: CollectionConfig = {
      slug: 'plans',
      admin: {
        useAsTitle: 'title',
        preview: (doc, { locale }) => {
            // Always return the same preview URL for the plans collection in each locale
            return `https://bigbird.com/preview/plans?locale=${locale}`;
        },
      },
      access: DEFAULT_ACCESS_CONTROL,
      fields: [
        {
          name: 'title',
          label: 'Title',
          type: 'text',
          localized: true,
        },
        {
          name: 'id',
          label: 'Custom ID',
          type: 'text',
        },
        buttons,
      ],
      timestamps: false,
    }
  • default discord avatar
    adam-mrozik
    11 months ago

    Ahh, noticed it will not appear, unless you have a slug field added. Could we adjust it? I want to have a preview button for collections which are all grouped on one page, so I do not require slug in some instances

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.