From 33da0473d4d1f34248abda5349deac05799c1048 Mon Sep 17 00:00:00 2001
From: Scott Reeves <Cottser@1167326.no-reply.drupal.org>
Date: Sun, 31 Jul 2016 11:37:13 -0400
Subject: [PATCH] =?UTF-8?q?Issue=20#2708255=20by=20dawehner,=20yoroy,=20sw?=
 =?UTF-8?q?entel,=20Jacine,=20G=C3=A1bor=20Hojtsy:=20Add=20support=20for?=
 =?UTF-8?q?=20#required=20on=20#type=20=3D>=20details?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 core/includes/form.inc                             |  1 +
 core/modules/system/src/Tests/Form/ElementTest.php | 14 ++++++++++++++
 core/modules/system/templates/details.html.twig    |  8 +++++++-
 .../tests/modules/form_test/form_test.routing.yml  |  6 ++++--
 .../src/Form/FormTestGroupDetailsForm.php          |  3 ++-
 .../src/Form/FormTestGroupFieldsetForm.php         |  3 ++-
 .../themes/classy/templates/form/details.html.twig |  8 +++++++-
 .../themes/stable/templates/form/details.html.twig |  8 +++++++-
 8 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/core/includes/form.inc b/core/includes/form.inc
index c98e9e83f0ba..d44f404cc2f2 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -254,6 +254,7 @@ function template_preprocess_details(&$variables) {
   $variables['description'] = (!empty($element['#description'])) ? $element['#description'] : '';
   $variables['children'] = (isset($element['#children'])) ? $element['#children'] : '';
   $variables['value'] = (isset($element['#value'])) ? $element['#value'] : '';
+  $variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
 
   // Suppress error messages.
   $variables['errors'] = NULL;
diff --git a/core/modules/system/src/Tests/Form/ElementTest.php b/core/modules/system/src/Tests/Form/ElementTest.php
index 684e13a09dad..0ae2edc49595 100644
--- a/core/modules/system/src/Tests/Form/ElementTest.php
+++ b/core/modules/system/src/Tests/Form/ElementTest.php
@@ -137,6 +137,20 @@ function testGroupElements() {
     $this->assertTrue(count($elements) == 1);
   }
 
+  /**
+   * Tests the #required property on details and fieldset elements.
+   */
+  public function testRequiredFieldsetsAndDetails() {
+    $this->drupalGet('form-test/group-details');
+    $this->assertFalse($this->cssSelect('summary.form-required'));
+    $this->drupalGet('form-test/group-details/1');
+    $this->assertTrue($this->cssSelect('summary.form-required'));
+    $this->drupalGet('form-test/group-fieldset');
+    $this->assertFalse($this->cssSelect('span.form-required'));
+    $this->drupalGet('form-test/group-fieldset/1');
+    $this->assertTrue($this->cssSelect('span.form-required'));
+  }
+
   /**
    * Tests a form with a autocomplete setting..
    */
diff --git a/core/modules/system/templates/details.html.twig b/core/modules/system/templates/details.html.twig
index cf50eb055e75..5014deb7be4f 100644
--- a/core/modules/system/templates/details.html.twig
+++ b/core/modules/system/templates/details.html.twig
@@ -17,8 +17,14 @@
  */
 #}
 <details{{ attributes }}>
+  {%
+    set summary_classes = [
+      required ? 'js-form-required',
+      required ? 'form-required',
+    ]
+  %}
   {%- if title -%}
-    <summary{{ summary_attributes }}>{{ title }}</summary>
+    <summary{{ summary_attributes.addClass(summary_classes) }}>{{ title }}</summary>
   {%- endif -%}
 
   {% if errors %}
diff --git a/core/modules/system/tests/modules/form_test/form_test.routing.yml b/core/modules/system/tests/modules/form_test/form_test.routing.yml
index 42a6ba49d9fb..790b9ce2105f 100644
--- a/core/modules/system/tests/modules/form_test/form_test.routing.yml
+++ b/core/modules/system/tests/modules/form_test/form_test.routing.yml
@@ -418,10 +418,11 @@ form_test.description_display:
     _access: 'TRUE'
 
 form_test.group_details:
-  path: '/form-test/group-details'
+  path: '/form-test/group-details/{required}'
   defaults:
     _form: '\Drupal\form_test\Form\FormTestGroupDetailsForm'
     _title: 'Group details testing'
+    required: FALSE
   requirements:
     _access: 'TRUE'
 
@@ -434,10 +435,11 @@ form_test.group_container:
     _access: 'TRUE'
 
 form_test.group_fieldset:
-  path: '/form-test/group-fieldset'
+  path: '/form-test/group-fieldset/{required}'
   defaults:
     _form: '\Drupal\form_test\Form\FormTestGroupFieldsetForm'
     _title: 'Group fieldset testing'
+    required: FALSE
   requirements:
     _access: 'TRUE'
 
diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php
index b38c978d9133..faa83cce9cac 100644
--- a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php
+++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php
@@ -20,11 +20,12 @@ public function getFormId() {
   /**
    * {@inheritdoc}
    */
-  public function buildForm(array $form, FormStateInterface $form_state) {
+  public function buildForm(array $form, FormStateInterface $form_state, $required = FALSE) {
     $form['details'] = array(
       '#type' => 'details',
       '#title' => 'Root element',
       '#open' => TRUE,
+      '#required' => !empty($required),
     );
     $form['meta'] = array(
       '#type' => 'details',
diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupFieldsetForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupFieldsetForm.php
index c9fb0260597c..fd7d83b8c173 100644
--- a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupFieldsetForm.php
+++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupFieldsetForm.php
@@ -20,10 +20,11 @@ public function getFormId() {
   /**
    * {@inheritdoc}
    */
-  public function buildForm(array $form, FormStateInterface $form_state) {
+  public function buildForm(array $form, FormStateInterface $form_state, $required = FALSE) {
     $form['fieldset'] = array(
       '#type' => 'fieldset',
       '#title' => 'Fieldset',
+      '#required' => !empty($required),
     );
     $form['meta'] = array(
       '#type' => 'container',
diff --git a/core/themes/classy/templates/form/details.html.twig b/core/themes/classy/templates/form/details.html.twig
index a4ce6f63dde0..24366c3d9683 100644
--- a/core/themes/classy/templates/form/details.html.twig
+++ b/core/themes/classy/templates/form/details.html.twig
@@ -16,7 +16,13 @@
 #}
 <details{{ attributes }}>
   {%- if title -%}
-    <summary{{ summary_attributes }}>{{ title }}</summary>
+    {%
+      set summary_classes = [
+        required ? 'js-form-required',
+        required ? 'form-required',
+      ]
+    %}
+    <summary{{ summary_attributes.addClass(summary_classes) }}>{{ title }}</summary>
   {%- endif -%}
   <div class="details-wrapper">
     {% if errors %}
diff --git a/core/themes/stable/templates/form/details.html.twig b/core/themes/stable/templates/form/details.html.twig
index f4c99e11113c..ccd2796016c2 100644
--- a/core/themes/stable/templates/form/details.html.twig
+++ b/core/themes/stable/templates/form/details.html.twig
@@ -15,8 +15,14 @@
  */
 #}
 <details{{ attributes }}>
+  {%
+    set summary_classes = [
+      required ? 'js-form-required',
+      required ? 'form-required',
+    ]
+  %}
   {%- if title -%}
-    <summary{{ summary_attributes }}>{{ title }}</summary>
+    <summary{{ summary_attributes.addClass(summary_classes) }}>{{ title }}</summary>
   {%- endif -%}
 
   {% if errors %}
-- 
GitLab