Skip to content

Object

An object field groups fields and stores their values together as one object. Use it for data that belongs together, like an address or a call to action, and to reuse such a group in several types.

import {Field} from 'alinea'

Field.object('Address', {
  fields: {
    street: Field.text('Street'),
    zip: Field.text('Zip code', {width: 0.5}),
    city: Field.text('City', {width: 0.5})
  }
})

Options

  • fields (required): the fields of the object, keyed by name. Any field type works, including lists and other objects.

  • The common options help, width, inline, initialValue, required, validate, readOnly, hidden and shared.

Value

An object with a key per field: {street: '', zip: '', city: ''}. New entries start with the initial values of the inner fields. Link and image fields inside the object are resolved when you query it, just like at the top level.

Querying

Select and sort on the object as a whole: the inner fields aren't registered in the schema, so Venue.address.city can't be used in select or orderBy. To filter on an inner value, use has:

import {cms} from '@/cms'
import {Config, Field} from 'alinea'

export const Venue = Config.document('Venue', {
  fields: {
    address: Field.object('Address', {
      fields: {
        street: Field.text('Street'),
        city: Field.text('City')
      }
    })
  }
})

const venuesInGhent = await cms.find({
  type: Venue,
  filter: {address: {has: {city: 'Ghent'}}},
  select: {address: Venue.address}
})

Reusing a group of fields

Like any field, an object instance can only be used once. Wrap it in a function to add the same group to several types:

import {Config, Field} from 'alinea'

function cta(label: string) {
  return Field.object(label, {
    fields: {
      label: Field.text('Label', {width: 0.5}),
      link: Field.link('Link', {width: 0.5})
    }
  })
}

export const Hero = Config.type('Hero', {
  fields: {
    title: Field.text('Title'),
    primary: cta('Primary button'),
    secondary: cta('Secondary button')
  }
})