Skip to content

Instantly share code, notes, and snippets.

@elliott-w
elliott-w / getPayload.ts
Last active November 30, 2025 21:10
A better way to fetch drafts in nextjs that automatically handles draft and status args
import config from '@payload-config'
import { draftMode } from 'next/headers'
import { getPayload as getPayloadFn, type CollectionSlug } from 'payload'
declare module 'payload' {
interface BasePayload {
draftModifications?: boolean
}
}
@elliott-w
elliott-w / middleware.ts
Created November 3, 2025 03:27
Middleware to Force Payload Login
import { env } from '@env'
import { jwtVerify } from 'jose'
import { NextRequest, NextResponse } from 'next/server'
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
// Skip middleware for static files and assets
if (isStaticFile(pathname)) {
return NextResponse.next()
@elliott-w
elliott-w / requireTranslationsPlugin.ts
Last active October 28, 2025 21:19
Payload CMS Require Translations Plugin - Prevents publishing until all translations have been added through draft autosaves
import {
traverseFields,
type CollectionConfig,
type CollectionSlug,
type GlobalConfig,
type GlobalSlug,
type Plugin,
type TraverseFieldsCallback,
type Validate,
} from 'payload'
@elliott-w
elliott-w / richTextStringifyPlugin.ts
Last active September 9, 2025 10:16
Payload CMS - Stores richText content as a string rather than a raw object
import {
traverseFields,
type CollectionConfig,
type GlobalConfig,
type Plugin,
type RichTextField,
type TraverseFieldsCallback,
} from 'payload'
import { richText } from 'payload/shared'
@elliott-w
elliott-w / ExampleCollection.ts
Last active May 14, 2025 06:53
Payload CMS - Custom Select field to select headings from rich text field
import { CollectionConfig } from 'payload'
import type { SelectHeadingFromRichTextFieldProps } from '../components/SelectHeadingFromRichTextField'
export const Articles: CollectionConfig = {
slug: 'articles',
fields: [
{
type: 'array',
name: 'tableOfContents',
labels: {
@elliott-w
elliott-w / payload.config.ts
Created May 6, 2025 09:45
Payload CMS assume relationships are resolved json schema
import { resolveRelationships } from './resolveRelationships'
export default buildConfig({
typescript: {
schema: [resolveRelationships],
},
})
@elliott-w
elliott-w / imageAspectRatios.ts
Last active April 24, 2025 21:47
Payload CMS Image Aspect Ratios Pugin
import type {
CollectionBeforeOperationHook,
FileData,
Plugin,
UploadCollectionSlug,
} from 'payload'
type AspectRatios = Record<string, number>
type Collections = Partial<Record<UploadCollectionSlug, AspectRatios>>
@elliott-w
elliott-w / collapse-acf-repeaters.php
Created September 12, 2024 03:43
Collapse acf repeater rows by default
<?php
// Add the "Collapse by default" checkbox to the repeater field settings
add_action('acf/render_field_settings', 'add_collapse_setting_to_repeater_field', 10, 1);
function add_collapse_setting_to_repeater_field($field) {
if ($field['type'] == 'repeater') {
acf_render_field_setting($field, array(
'label' => __('Collapse by default', 'text-domain'),
'instructions' => __('Collapse all rows by default when editing.', 'text-domain'),
'type' => 'true_false',
@elliott-w
elliott-w / CustomAdminView.tsx
Last active August 9, 2024 12:11
Payload v3 Custom Form in Admin
import { FC } from 'react'
import { Form, FormSubmit, Gutter, RenderFields } from '@payloadcms/ui'
import { mapFields } from '@payloadcms/ui/utilities/buildComponentMap'
import { AdminViewProps, Field, WithServerSidePropsComponentProps } from 'payload'
import { WithServerSideProps as WithServerSidePropsGeneric } from '@payloadcms/ui/shared'
import { DefaultTemplate } from '@payloadcms/next/templates'
import { buildStateFromSchema } from '@payloadcms/ui/forms/buildStateFromSchema'
type WithServerSidePropsPrePopulated = React.FC<
Omit<WithServerSidePropsComponentProps, 'serverOnlyProps'>
@elliott-w
elliott-w / CustomAdminView.tsx
Last active August 2, 2024 10:18
Payload CMS Custom Admin View Form
import { FC, Fragment } from 'react'
import {
Form,
FormSubmit,
Gutter,
HydrateClientUser,
RenderFields,
SetStepNav,
SetViewActions,
} from '@payloadcms/ui'