Fields

Array

A list of items that all have the same fields.

Some content repeats (think the slides of a carousel, the members of a team, etc.). An array is a list of items with the same fields and different then blocks, which are (more) dynamic.

The array field in the admin

Example

cms/globals/team.ts
export const team = defineGlobal({
  fields: {
    members: {
      type: "array",
      maxRows: 12,
      itemLabels: { singular: "Member", plural: "Members", title: "{name} ({role})" },
      fields: {
        name: { type: "text", required: true },
        role: { type: "text" },
        photo: { type: "image" },
      },
    },
  },
});

A read returns a list of objects. Each item has a stable id, which the admin keeps across saves:

team.members;
// [
//   { id: "a1", name: "Ada", role: "Chef", photo: { … } },
//   { id: "b2", name: "Linus", role: "Baker", photo: null },
// ]

Render it

---
import { Image } from "tenoncms/astro/components";
---

<ul class="team">
  {team.members?.map((member) => (
    <li>
      {member.photo && <Image image={member.photo} sizes="160px" />}
      <strong>{member.name}</strong>
      <span>{member.role}</span>
    </li>
  ))}
</ul>

Use the item's id as the key. It stays the same when editors sort the list.

Item labels

itemLabels names the items in the admin:

  • singular names one item, in the row header and the add button: "Add Member".
  • plural is the heading of the list. It defaults to the field's label.
  • title builds the row title from the item's fields, like "{name} ({role})". Without it, the row shows the first text of the item.

Tree appearance

appearance: "tree" shows the items as compact rows that open an edit sheet. It fits long lists and lists that nest, like a menu. Menus use it.

Limits

  • You cannot filter by fields inside a list.
  • A field inside an item MUST NOT be named id.
  • join and unique do not work inside items.
  • In a localized collection, a top-level list is localized as a whole. Fields inside items CAN NOT set localized. See Localization.
  • Errors name the position of the item, like members.2.name.

Options

The fields of each item. Items can hold groups, arrays and blocks, but no joins.The fewest items. required means at least one.The most items.The names of the items and the list in the admin. See Item labels.Show the items as compact rows with an edit sheet. By default, each item is edited inline.A tab bar for the fields of each item. A field picks its tab with tab.Boxes around fields of each item. A field picks its box with fieldset.