Skip to content

Composite element

A composite element repeats a group of inputs. Pass an associative array of children in #element. Each key is the name of a value.

$form['contacts'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Contacts'),
'#element' => [
'first_name' => [
'#type' => 'textfield',
'#title' => $this->t('First name'),
],
'last_name' => [
'#type' => 'textfield',
'#title' => $this->t('Last name'),
],
],
'#default_value' => [
['first_name' => 'John', 'last_name' => 'Smith'],
['first_name' => 'Jane', 'last_name' => 'Doe'],
],
];

Two rows with a first name and a last name field stacked in one cell, each with its label.

The value is a list of arrays:

[
['first_name' => 'John', 'last_name' => 'Smith'],
['first_name' => 'Jane', 'last_name' => 'Doe'],
]
  • Without #header, the children are stacked in one cell and keep their labels. To get one column per child, use #header. See Composite with header.
  • A row is dropped when every child is empty.
  • A child can be any form element, also a container. See Nested containers.
  • Related properties: #element, #default_value.