Skip to content

Composite with header

#header turns a composite element into a real table with one column per child. It also controls the header row. Every example below uses the same two children.

$children = [
'textfield' => [
'#type' => 'textfield',
'#title' => $this->t('Name'),
],
'email' => [
'#type' => 'email',
'#title' => $this->t('Email'),
],
];

Without #header, one header cell spans the table and the children share one cell per row.

$form['contacts'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Contacts'),
'#element' => $children,
];

A table with one empty header cell and the Name and Email children stacked in the row, each with its label.

One column per visible child. The column title is the #title of the child. The titles are hidden inside the cells.

$form['contacts'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Contacts'),
'#header' => TRUE,
'#element' => $children,
];

A table with the column titles Name and Email and one input per cell.

A single header cell spans all columns.

$form['contacts'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Contacts'),
'#header' => $this->t('Header text'),
'#element' => $children,
];

A table with one header cell that spans both columns and reads Header text.

Give one entry per visible child. An entry is a string or a header cell array, for example ['data' => $this->t('Full name'), 'width' => '30%'] to size the column.

$form['contacts'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Contacts'),
'#header' => [
$this->t('Full name'),
$this->t('Email address'),
],
'#element' => $children,
];

A table with the custom column titles Full name and Email address.

A single header cell with your text, spanning all columns.

$form['contacts'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Contacts'),
'#header' => TRUE,
'#header_label' => $this->t('Header label'),
'#element' => $children,
];

A table with one header cell that reads Header label.