How to create a collapsible widget on Drupal 8
There's a new render element type named details
which is for creating a collapsible widget in Drupal 8. details
is similar to the existing fieldset
Form API element but details
can be used even outside of forms.
The following render array $build
builds a collapsible widget shown in the image below.
$build['author'] = [
'#type' => 'details',
'#title' => $this->t('Author'),
];
$build['author']['name'] = [
'#type' => 'textfield',
'#title' => $this->t('Name'),
];
This sample is taken from the official d.o. page linked below.