# links One link shape for the whole project — CTA buttons, links in cards, downloadable files. A schema field + a function that turns the value into an `href`. The look (the `CMSLink` component, button variants) stays in the project. ## Fields ```ts import { buildLinkField, buildLinksField } from '@intecion/ipal-kit' fields: [ buildLinksField(), // `links`: 0–2 section buttons buildLinksField({ maxRows: 3, appearances: ['primary', 'secondary'] }), buildLinkField(), // `link`: a single link buildLinkField({ disableLabel: true, appearances: false }), // whole card is clickable ] ``` | Option | Default | What it does | |---|---|---| | `name` | `'link'` / `'links'` | field name | | `relationTo` | `'pages'` | collection for the "Page" type; an array = polymorphic relation (below) | | `uploadTo` | `'media'` | collection for the "File" type (PDF, documents) | | `appearances` | `['primary','secondary','link']` | styles to pick from — **any project names**, e.g. `['primary', { label: 'Highlighted', value: 'highlight' }]` (a menu with a highlighted item, a button in the brand color); `false` = no field | | `disableLabel` | `false` | no label (the link wraps a card / image) — `buildLinkField` only | | `required` | `false` | target and label fields are required | | `interfaceName` | `CmsLink` / `CmsLinkPlain` / `CmsLinkNoLabel` / `CmsLinkPlainNoLabel` | type name in `payload-types.ts` | | `maxRows` | `2` | `buildLinksField` only | | `labels` | `{ singular: { en: 'Button', pl: 'Przycisk' }, plural: { en: 'Buttons', pl: 'Przyciski' } }` | `buildLinksField` only — admin panel: "Add Button"; a list of links: `{ singular: 'Link', plural: 'Links' }` (a string or `{ en, pl }`) | `buildLinksField` has a built-in `rowLabel`: a collapsed row shows the button label ("Contact us"), not "Button 01". It needs an importMap entry — `pnpm payload generate:importmap` after updating the plugin. ## Readable rows in other arrays — `rowLabel` ```ts import { rowLabel } from '@intecion/ipal-kit' { name: 'items', type: 'array', labels: { singular: 'Feature', plural: 'Features' }, // "+ Add Feature" admin: { initCollapsed: true, components: rowLabel('heading', 'Feature') }, fields: [/* … */], } ``` `rowLabel(fields, fallback = 'Item')` — paths within the row (e.g. `'heading'`, `['name', 'link.label']`), the first non-empty one wins (relation/upload → `title`/`name`/`label`/`alt`/`filename`); all empty → "Feature 02" (`{fallback} {number}`). Component: `@intecion/ipal-kit/admin#RowLabel`. Admin panel rules: AGENT.md §6.5a. Link value: `type` (`internal` page — default · `external` URL `https://`/`http://`, `mailto:`, `tel:`, `#anchor` — validated in the admin panel · `file` upload), `page`, `url`, `file`, `label` (localized), `newTab`, `appearance`. `payload-types.ts` gets the type **`CmsLink`**; a link with no style (`appearances: false`) has the type `CmsLinkPlain`, without a label (`disableLabel`) — `CmsLinkNoLabel`, both at once — `CmsLinkPlainNoLabel` (since 1.11.1; before that all were called `CmsLink` and the type depended on field order). Each of them fits `CmsLink`, so `CMSLink` accepts them unchanged. Different `appearances` or `relationTo` lists in one project → give it your own name: `buildLinkField({ interfaceName: 'CategoryLink', … })`. A downloadable file is a `file` type link — **not** a separate `pdfUrl` field. ## Links to collection entries (1.11.0) Page, category, product — one field: `buildLinkField({ relationTo: ['pages', 'categories', 'products'] })` (a polymorphic relation; one collection = as before). A collection entry with `content` resolves to `/{locale}/{archive-page}/{entry}` — `resolveLink` / `resolveNav` / `getLinkOptions` from `lib/content` take the archives from Site settings themselves (`archives: { categories: 'kategorie' }`). Without lib/content: `resolveLinkHref(link, { config, locale, archives, pagesSlug })`. An entry with no assigned archive → `null` (the link does not render). The entry path is built by `buildEntryPath`, which **always** adds `/{locale}` — on a single-language site a link to an entry gets a needless `/pl/…` prefix (`src/modules/links/resolveLinkHref.ts` ~94). Changing `relationTo` to an array changes the data shape (`page` → `{ relationTo, value }`): existing links must be migrated (iapl-migrate-fields). ## href ```ts import { resolveLinkHref } from '@intecion/ipal-kit' resolveLinkHref(link, { locale, config: i18nConfig, homeSlug }) // internal → '/en/about' (home page → '/en') · external → URL · file → file URL // null → empty link, no page in this language, page not populated (ID only), no file, // URL outside https?:// mailto: tel: sms: # / (e.g. javascript:) — safeHref ``` Options: `locale`, `config` (required), `homeSlug`, `pagesSlug` (`'pages'`), `archives` (`{ collection: archiveSlug }`). `safeHref(url)` is exported separately (URL filter). The file URL (`file.url`) does not go through `safeHref`. Internal links need a **populated** page (`depth ≥ 2` in a block). `homeSlug` — the home page slug (a string or a per-language map, e.g. from `SiteSettings.homepage`), so it collapses to `/{locale}`. ## Render-ready link — `resolveLink` ```ts // in a server component, from lib/content (locale → homeSlug and i18n by itself): const l = await resolveLink(link, locale) // without lib/content: resolveLink(link, { locale, config: i18nConfig, homeSlug }) // → { href, label, internal, newTab, targetProps, appearance } | null ``` - `internal` → `next/link`, otherwise `` (URL, file, mailto, tel), - `targetProps` → `{ target: '_blank', rel: 'noopener noreferrer' }` for a new tab (always for files), - `appearance` → the variant key in the project's `Button.tsx`, - `null` → the link leads nowhere in this language (no page, no file, unsafe URL) — render nothing. `CMSLink` stays in the project (the element and classes are the look), but computes nothing itself: ```tsx // src/components/ui/CMSLink.tsx import Link from 'next/link' import type { ReactNode } from 'react' import type { CmsLink } from '@/payload-types' import { resolveLink } from '@/lib/content' import { buttonClass } from './Button' export async function CMSLink({ link, locale, className, children }: { link: CmsLink | null | undefined; locale: string; className?: string; children?: ReactNode }) { const l = await resolveLink(link, locale) const content = children ?? l?.label if (!l || !content) return null const cls = [l.appearance ? buttonClass(l.appearance as never) : '', className].filter(Boolean).join(' ') || undefined return l.internal ? {content} : {content} } ``` **Do not write your own version** (`resolveHref`, `getHref`, `` `/${slug}` ``, `slug === 'home'`): it loses the language prefix, the home page from Site settings, the `javascript:` filter and dead links. `iapl-audit` reports it as an ERROR. A field in a different shape (e.g. `page` + `url`) is mapped onto the plugin's shape instead of computing the href: `resolveLink(url?.trim() ? { type: 'external', url } : { type: 'internal', page }, locale)`. Highlight, color, item variant = **a style from the admin panel** (`appearances`), mapped to classes in the project (`l.appearance === 'highlight'` → class). Never logic based on content (`label === 'Tesla'`) — the editor renames it and the style disappears; `iapl-audit` reports it as an ERROR. ## Legal links — `getSystemLinks` Default roles: `privacyPolicy`, `cookiePolicy`, `termsOfService`. ```ts const legal = await getSystemLinks(locale) // from lib/content // [{ role: 'privacyPolicy', href: '/pl/polityka-prywatnosci', label: 'Polityka prywatności', internal, targetProps }, …] await getSystemLinks(locale, ['privacyPolicy', 'termsOfService']) // selected roles // without lib/content: resolveSystemLinks(settings, { locale, config: i18nConfig, homeSlug, roles }) ``` Pages are assigned in Site settings → System pages; **the label is the page title** in the given language, so there is no text in code (a hard-coded "Polityka prywatności" breaks the EN version). An unassigned role → skipped. ## Menu — `resolveNav` ```ts const items = await resolveNav(header.navItems, locale) // from lib/content // resolveNav(items, { locale, config: i18nConfig, homeSlug, linkField: 'link', childrenField: 'subItems' }) // → NavItem[]: { href, label, internal, newTab, targetProps, appearance?, children: NavItem[] } // defaults linkField: 'link', childrenField: 'subItems' ``` Any depth, configurable item shape (`linkField`, `childrenField`), items with a dead link or no label are skipped (instead of `href="#"`). The result is plain data — it goes straight to the client `MobileNav`. Menu markup (Header, Footer, MobileNav) stays in the project: [nav-code.md](./agent/skills/iapl-nav/resources/nav-code.md). `homeSlugFrom(settings)` — the home page slug from `SiteSettings.homepage` (lib/content: `getHomeSlug(locale)`, and the full link option set: `getLinkOptions(locale)` — also for `RenderForm linkOptions`). ## Migration from `src/fields/link.ts` Projects from before 1.8.x have a copy in `src/fields/link.ts` and `src/lib/links.ts`. The data shape is identical (`interfaceName: 'CmsLink'`), so **no database migration is needed**: 1. `linkField` / `linksField` imports from `@/fields/link` → `buildLinkField` / `buildLinksField` from `@intecion/ipal-kit`, 2. `resolveLinkHref(link, locale, homeSlug)` from `@/lib/links` → `resolveLinkHref(link, { locale, config: i18nConfig, homeSlug })` from the plugin, 3. delete `src/fields/link.ts` and `src/lib/links.ts`, `pnpm generate:types`. 4. `lib/navigation.ts` with its own `resolveNav` → `resolveNav` from `lib/content` (same data + `internal`, `targetProps`; `subItems` is now called `children`). ## Turn off, override, extend - The module has no option in `iapl()` — you use the fields and resolvers in your collections or you don't. - Fields: `name`, `relationTo` (a string or an array), `uploadTo`, `appearances` (your own names or `false`), `disableLabel`, `required`, `interfaceName`; `buildLinksField` also `maxRows`, `labels`. - Link types (`internal` / `external` / `file`) and the allowed URL schemes are currently fixed; map a field of a different shape onto `LinkValue` before `resolveLink`. - Building blocks for your own rendering: `resolveLinkHref`, `resolveLink`, `resolveNav` (`linkField`, `childrenField`), `resolveSystemLinks` (`roles`), `homeSlugFrom`, `safeHref`. - The look (`CMSLink`, button variants) is always on the project side.