Skip to content

Dynamic compound fields

Experimental

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

The Compound Field: Dynamic sub-module (compound_field_dynamic) adds the Dynamic compound field type. Site builders set it up as described in mix item types in one list. This page covers its storage, rendering and the code you can rely on.

Storage

A dynamic field stores one entry per item, in order. Each entry has three properties:

Property Holds
uuid The item's cfi_uuid, or a container's own UUID.
field_name The compound field that holds the item, or :container: followed by the container's key.
parent_uuid The UUID of the entry it sits inside; empty at the top level.

The values stay in the item types: each is an ordinary unlimited compound field, and an entry points at one of its items by cfi_uuid. Saving the host syncs the two. Entries whose item is gone are dropped, items that no entry lists are added, and items that cannot stay inside their parent move to the top level. Dropped and moved entries are logged as warnings in the compound_field_dynamic channel.

A parent kept only for the items inside it is an entry without an item: its compound field stores no row for it. When it gets values later, its item reuses the entry's UUID.

Queries and Views

Views, entity queries and the item model read the item types as ordinary compound fields. They see every item, in the item type's own delta order, without the dynamic order or the nesting. A parent kept only for its items does not appear there. Read the dynamic field's entries for the order and nesting.

Rendering

The Dynamic compound (ordered) formatter renders each item type once with its own formatter, then wraps each item in a compound_field_dynamic_item element in the dynamic field's order. Items inside a parent render inside its element. An item type with no formatter in the chosen view mode falls back to the compound field formatter, without a notice.

The template receives:

Variable Holds
content The item's rendered output. Empty for a container or a kept parent without values.
child_items The rendered items inside it.
children_heading The heading set under Nesting, or Items. Empty when nothing renders inside it.
ordering_field The dynamic field's machine name.
member_field The item type's field name. Empty for a container.
compound_id The compound's ID. Empty for a container.
container_key The container's key. Empty for an item.
is_container Whether the entry is a container.
is_empty Whether a container has no visible items inside.
depth 0 at the top level, 1 inside a parent.
view_mode The view mode of the display that shows the dynamic field, not the item type's view mode.

The default template prints neither children_heading nor a container's label: both are editing aids. Template suggestions, from least to most specific among each group:

  • compound-field-dynamic-item--ORDERING.html.twig and compound-field-dynamic-item--ORDERING--depth-N.html.twig
  • for a container: compound-field-dynamic-item--container.html.twig and compound-field-dynamic-item--container--KEY.html.twig
  • for an item: compound-field-dynamic-item--MEMBER.html.twig, compound-field-dynamic-item--COMPOUND.html.twig and compound-field-dynamic-item--MEMBER--VIEW-MODE.html.twig

The wrapper carries compound-field-dynamic-item plus classes for a container and its key, the item type, the compound, an empty container and the depth. A container key is eight lowercase hexadecimal characters, made when the container is added.

Code

The sub-module defines no hooks of its own. Use these public methods of the Drupal\compound_field_dynamic\DynamicCompoundResolver service to read the setup:

  • orderingFieldFor($entity_type_id, $bundle, $member): the dynamic field that uses an item type. With two claiming fields, the one whose machine name sorts first.
  • owns($ordering, $member): whether a dynamic field is that one.
  • members($ordering) and containers($ordering): the item types and containers it lists.
  • locate($host, $member, $uuid): the delta of an entry's item in its item type.

Like the rest of the module's PHP, these may change between pre-alpha versions.

Entity validation, which JSON:API and REST run and $entity->validate() returns, checks the entries. It reports an entry that names a field that is not an item type ("%field is not an item type of this field."), a repeated item ("This item is listed more than once."), broken nesting ("%child cannot be inside %parent.", "The parent of %child is not in this list.", "%parent needs at least 1 %child item.") and a kept parent whose type has a required sub-field ("%parent needs its required fields filled."). A plain $entity->save() does not validate: the sync drops unknown and repeated entries and moves misplaced items to the top level, with a logged warning. Limits are never validated. A save that goes over a limit is kept and logs a warning, and so does a save that puts a type at the top level where it is not allowed.

The rules for item types, unlimited, not required and not translatable, are also enforced when a field or its storage is saved through code. During a configuration import, a required or translatable item type and invalid dynamic field settings are logged as warnings instead; limiting an item type's cardinality is refused even then. Enabling translation for the bundle through code or a language.content_settings configuration import is not checked yet.