Skip to content

Mix item types in one list

Experimental

The Compound Field: Dynamic sub-module is experimental. Its configuration, storage and APIs may change without an upgrade path, so don't use it on production sites yet.

A dynamic compound field puts the items of several compound fields into one ordered list. Editors add sessions, notices and tracks in any order and can place items inside a parent, one level deep. Each item stays a row of its own compound field; there are no child entities.

When to use it

Model Use it when
Compound field Every item has the same shape.
Dynamic compound field Editors mix several item shapes in one order, optionally one level deep.
Paragraphs Items need their own URL, publication state, revision history or translation, or nest deeper than one level.

The dynamic field stores only the order and the nesting. The values stay in the compound fields it lists, which this page calls item types.

Prepare the item types

Start with the Conference session example and enable the Compound Field: Dynamic module (compound_field_dynamic).

  1. Under Structure → Compound fields, create Track with one string, Track name, and Notice with one formatted text, Message.
  2. Open Structure → Content types → Conference → Manage fields. Add Track as Tracks and Notice as Notices. Sessions already exists.
  3. Give each of the three fields Unlimited values. Leave Required and Users may translate this field cleared.

An item type follows three rules:

  • Unlimited values. The dynamic field decides how many items of each type exist, so the item type cannot cap them.
  • Not translatable. Dynamic compound fields do not support translation yet; see translation.
  • Not required. Editors never fill an item type on its own: the dynamic field writes it. A required item type would demand at least one item of that type on every conference. The form would save without one, while entity validation, which JSON:API and REST run, would refuse the same content, and editors could not remove the last item of that type. To require content, make the dynamic field required, or set a Minimum under Nesting.

Add the dynamic field

  1. On Conference → Manage fields, select Create a new field → Compound field → Dynamic compound.
  2. Label it Schedule. The field always holds an unlimited number of items.
  3. Save. The field settings open with the Item types table.

Choose the item types

Each compound field of the bundle that can be an item type has a row:

  • Enabled lets editors add items of the type.
  • Label renames the type for editors. Empty, the type uses its field's label.
  • Top level lets editors add the type outside a parent. Clear it for a type that only goes inside another one.
  • Top level limit caps how many items of the type sit at the top level. Empty means no limit.
  • Drag the rows to set the order of the add options editors see.

Enable Tracks, Sessions and Notices. Each enabled type needs a name of its own; otherwise the form reports "Another item type is already named @name." At least one enabled type must be allowed at the top level.

Add container adds a row for a container: a parent with no content of its own that groups other items. A container's label is optional; editors see Container without one. Containers always sit at the top level, and its Remove button deletes the container. Items inside a removed container move to the top level.

Unavailable compound fields

Compound fields of the bundle that cannot be item types are listed under Unavailable compound fields, with every reason that applies, in this order:

  • "used by @field": another dynamic field already lists it.
  • "limited to @count items; item types need unlimited".
  • "required; item types can't be required".
  • "translatability doesn't match this field".

A field with several reasons shows them as a nested list. Each field name links to its field settings when you may edit them. Fix the field there and reload the dynamic field's settings to see it in the table.

The section stays closed while a compound field can be added as an item type. When none can, the section opens and the form warns "No compound field is available. See Unavailable compound fields below." The Item types table then shows only containers and types the field already lists, and is hidden when there are none. A bundle without any compound field gets "Add a compound field with unlimited cardinality to this bundle first."

Nest items

The Nesting section appears once a type is enabled. It has one part per type or container that can hold items:

  • Heading is the title editors see above the items inside it. Empty, it reads Items.
  • Allowed chooses the types that may go inside it.
  • Limit caps the items of that type inside one parent. Empty means no limit.
  • Minimum requires that many items of the type inside each parent.

For the example, allow Sessions inside Tracks, and clear Top level for Sessions if every session belongs to a track.

Nesting is one level deep. A type is either a parent or a child, never both, and a parent always stays allowed at the top level. The settings form refuses other combinations with an error on the setting to change.

Configure the widget

Open Conference → Manage form display. The Schedule widget is Dynamic compound list:

  • Add mode: Buttons (the default) shows one add button per type. Select list shows a type list and one add button. Dropdown button shows one button with the other types in its menu.
  • Default item type: the item type or container the select list starts on and the dropdown button leads with.
  • Allow moving items between parents: off by default. On, each item that may go inside a parent gets a Move to… list.
  • Open by default, Start closed from this many items and Auto-collapse work as on a compound field; see widgets and formatters. Open by default is off for a dynamic field, so items start closed.

Each item uses its own compound field's widget settings: layout, sub-field widgets and the item summary. The item types disappear from the form as separate fields, and Manage form display notes that they are edited through Schedule.

Configure the display

Open Conference → Manage display. The Schedule formatter is Dynamic compound (ordered). Choose a view mode per item type; each item renders with the formatter its compound field has in that view mode. Create a view mode, such as Component, to keep those formatters apart from the default display.

The item types no longer render on their own, and Manage display notes that they are rendered through Schedule. A formatter that renders all items as one block, such as a table, cannot be split per item: Schedule renders that type with the compound field formatter instead and logs a notice.

In Layout Builder, place only the Schedule block. A block for an item type renders its items a second time, outside the schedule order.

Change the item types later

Clearing Enabled only stops editors adding new items of that type. Existing items keep their place, render, and stay editable.

Release removes a type from the dynamic field, with its nesting settings, when you save. It appears in the Operations column for a type that is not enabled and that no content uses, including older revisions. Removing the items from the content is not enough while older revisions still hold them: delete that content, or keep the type disabled. Checking Enabled again before you save cancels the release. A saved release is final: listing the field again later starts from fresh settings.

The settings form refuses a compound field that another dynamic field already lists. If two dynamic fields list the same one anyway, for example from older configuration, the settings of both warn about it, and only the dynamic field whose machine name sorts first uses it. The other one shows its existing items locked and does not render them. Clear the type in one of them.

Deleting a compound field that is an item type removes its items from the dynamic field. Its field delete form says so.

Rules for item-type fields

The field settings of an item type refuse a save that breaks one of its rules:

  • "%label is an item type of %field and must keep unlimited cardinality."
  • "%label is an item type of %field and can't be translatable: dynamic compound fields don't support translation yet."
  • "%label is an item type of %field and can't be required."

A save that fixes a problem always goes through, one setting at a time, and so does a save that changes nothing.

An item type that stops meeting the rules, for example after a configuration import or after translation is turned on for the bundle through code, keeps its row in the Item types table and is also listed as unavailable. Its Enabled box can be cleared but not checked again, and Release appears once no content uses it. While the type is still enabled, saving the dynamic field is refused with "@label (@name) can't be an item type: @reason." followed by what to do. A type listed by two dynamic fields is the exception: it follows the warning described under Change the item types later.

Translation

Dynamic compound fields do not support translation yet. Neither the dynamic field nor its item types can be translatable:

  • The dynamic field's own settings refuse Users may translate this field: "Translatable dynamic compound fields are not supported yet."
  • Configuration → Regional and language → Content language and translation refuses them too: "%label can't be translatable: dynamic compound fields don't support translation yet. Uncheck it to save." For an item type, the message also names its dynamic field.

The other fields of the content type can still be translated. See translation and moderation.

Current limitations

  • Nesting is one level deep.
  • Nested items are cramped on narrow screens. Around 1024 pixels wide, close the admin theme's edit sidebar for more room. On phones, nested items stay cramped; see core issue #2264739.
  • Limits apply in the form. Content saved through code or an API with more items is kept, and the save logs a warning.
  • A parent kept only for the items inside it has no values of its own, so Views and other queries on its compound field do not see it; see dynamic compound fields for developers.

Add and arrange mixed items.