3 namespace Drupal\Core\Render\Element;
5 use Drupal\Core\Render\Element;
8 * Provides a render element for a details element, similar to a fieldset.
10 * Fieldsets can only be used in forms, while details elements can be used
11 * outside of forms. Users click on the title to open or close the details
12 * element, showing or hiding the contained elements.
15 * - #title: The title of the details container. Defaults to "Details".
16 * - #open: Indicates whether the container should be open by default.
18 * - #summary_attributes: An array of attributes to apply to the <summary>
23 * $form['author'] = array(
24 * '#type' => 'details',
25 * '#title' => $this->t('Author'),
28 * $form['author']['name'] = array(
29 * '#type' => 'textfield',
30 * '#title' => $this->t('Name'),
34 * @see \Drupal\Core\Render\Element\Fieldset
35 * @see \Drupal]Core\Render\Element\VerticalTabs
37 * @RenderElement("details")
39 class Details extends RenderElement {
44 public function getInfo() {
45 $class = get_class($this);
48 '#summary_attributes' => [],
51 [$class, 'processGroup'],
52 [$class, 'processAjaxForm'],
55 [$class, 'preRenderDetails'],
56 [$class, 'preRenderGroup'],
58 '#theme_wrappers' => ['details'],
63 * Adds form element theming to details.
66 * An associative array containing the properties and children of the
70 * The modified element.
72 public static function preRenderDetails($element) {
73 Element::setAttributes($element, ['id']);
75 // The .js-form-wrapper class is required for #states to treat details like
77 static::setAttributes($element, ['js-form-wrapper', 'form-wrapper']);
79 // Collapsible details.
80 $element['#attached']['library'][] = 'core/drupal.collapse';
82 // Open the detail if specified or if a child has an error.
83 if (!empty($element['#open']) || !empty($element['#children_errors'])) {
84 $element['#attributes']['open'] = 'open';
87 // Do not render optional details elements if there are no children.
88 if (isset($element['#parents'])) {
89 $group = implode('][', $element['#parents']);
90 if (!empty($element['#optional']) && !Element::getVisibleChildren($element['#groups'][$group])) {
91 $element['#printed'] = TRUE;