Use property keys for custom_field theming

Created on 17 June 2025, 10 days ago

Problem/Motivation

The custom_field template has an "items" variable, which contains an array of subfield / property render arrays.
The default implementation of templates/custom-field.html.twig just loops over each subfield / property and renders it.

When trying to create an override template, we might want to render some subfields specifically, but that is currently not possible (except by doing some inspection of the array, not that easy to do in Twig), because the items array just has numeric keys.

Preferably the "items" array would not have numeric keys, but use the subfield / property key.
This way, specific properties can be correctly extracted from the "items" array, to specifically render it (e.g. when using SDC, each custom_field property could be an SDC "prop" or "slot".

Steps to reproduce

- Create a custom_field named "field_something" (unlimited values) with 2 properties, e.g:
- "heading": Text (plain) 255 - not required
- "text": Text (formatted) - not required
- Override the specific Twig template for this field - e.g. custom-field--field-something.html.twig:

<h2>{{ item[0] }}</h2>
<p>{{ item[1] }}</p>

- Use this field somewhere (e.g. on a node type), and fill in both properties. It works as expected.
- Use this field somewhere else. and just fill in the "text" property. --> Now the body text is printed within the h2 tag...

Proposed resolution

Allow writing the example above as follows:

<h2>{{ item['heading'] }}</h2>
<p>{{ item['text'] }}</p>
✨ Feature request
Status

Active

Version

3.1

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium svendecabooter Gent

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024