Files
ipal-kit/docs/links.md
T
Radosław SmolińskiandClaude Opus 5.5 2d9f3e15d4 Docs in English (part 1), seeding guide, Coolify deployment
- Translate to English every doc the code branches did not touch: AGENT.md,
  developer guide, vibe, CHANGELOG, docs index, module pages, all skills
  except ipal-seo-check (14 pages follow after the security/SEO merge).
  Placeholder marker is now [TO FILL: …]; skill resources renamed to
  section-code.md / nav-code.md. AGENT.md and the ipal-skills pointer
  block say the docs are in English.
- docs/seeding.md: create-only, natural keys, never delete, no users or
  secrets, empty-field globals, fallbackLocale: false, dry run, backup and
  restore, production update guard, seeds vs migrations, Coolify Terminal.
  Seed templates gain --dry-run, the SEED_UPDATE production guard
  (IPAL_SEED_ALLOW_PROD_UPDATE), skip of edited records and a summary.
  "Seeding on production" rule block in AGENT.md.
- Coolify is the single deployment path (deployment, env, developer guide,
  ipal-setup): build vs runtime env, migrations on container start
  (prodMigrations), health check and rolling updates, rollback, backups and
  restore, DNS/Let's Encrypt/CAA. R2: custom domain needs a Cloudflare zone;
  default is media served by the app (R2_PUBLIC_URL empty).
- docsConsistency: no Polish prose outside code/quoted data (pending pages
  listed); IPAL_* names may also be read by shipped skill templates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:39:03 +02:00

184 lines
10 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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: 'Button', plural: 'Buttons' }` | `buildLinksField` only — admin panel: "Add Button"; a list of links: `{ singular: 'Link', plural: 'Links' }` |
`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 (ipal-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 `<a>` (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
? <Link href={l.href} className={cls} {...l.targetProps}>{content}</Link>
: <a href={l.href} className={cls} {...l.targetProps}>{content}</a>
}
```
**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. `ipal-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; `ipal-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/ipal-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 `ipalKit()` — 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.