Skip to content

Date and datelist children

Date elements work as #element. A datelist renders several selects in a row and its value is a date object.

$form['dates'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Dates'),
'#element' => [
'#type' => 'datelist',
'#title' => $this->t('Date'),
'#title_display' => 'invisible',
'#date_timezone' => 'UTC',
'#date_part_order' => ['year', 'month', 'day', 'hour', 'minute'],
],
];

A list with one row. The row holds selects for year, month, day, hour and minute.

The value is a list of DrupalDateTime objects, one for each filled row. Convert them before you store them.

foreach ($form_state->getValue('dates') as $date) {
$timestamp = $date->getTimestamp();
}
  • Date elements can also be children of a composite element.
  • Related property: #element.