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,hiddenandshared.
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
Without a select the object is returned as a whole. Select or sort on a field inside it through the object, Venue.address.city, and filter on inner values with has:
import {cms} from '@/cms'
import {Config, Field, Query} 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: {title: Query.title, street: Venue.address.street},
orderBy: {asc: Venue.address.street}
})
// Array<{title: string, street: string}>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')
}
})