diff --git a/core/includes/form.inc b/core/includes/form.inc index efbac995cd9690f8922b23e0bc2994168d87b42f..aaaeb1707afee05c1eadb256ce302efb82b193f4 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -257,6 +257,12 @@ 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'] : ''; + + // Display any error messages. + $variables['errors'] = NULL; + if (!empty($element['#errors']) && empty($element['#error_no_message'])) { + $variables['errors'] = $element['#errors']; + } } /** diff --git a/core/modules/system/src/Tests/Form/ElementTest.php b/core/modules/system/src/Tests/Form/ElementTest.php index 6555a4f063fbf592ffb069b2a6f572982b23e334..b7295ff49e19ef4faf17be5b7d630b5c6174f189 100644 --- a/core/modules/system/src/Tests/Form/ElementTest.php +++ b/core/modules/system/src/Tests/Form/ElementTest.php @@ -160,4 +160,12 @@ public function testFormAutocomplete() { $this->assertEqual(count($result), 1, 'Ensure that the user does have access to the autocompletion'); } + /** + * Tests form element error messages. + */ + public function testFormElementErrors() { + $this->drupalPostForm('form_test/details-form', [], 'Submit'); + $this->assertText('I am an error on the details element.'); + } + } diff --git a/core/modules/system/templates/details.html.twig b/core/modules/system/templates/details.html.twig index 1c0fd795eba1d35607d20b00ef25308221cb6aa4..cf50eb055e75588b92ec7f06823d366e0590395f 100644 --- a/core/modules/system/templates/details.html.twig +++ b/core/modules/system/templates/details.html.twig @@ -5,6 +5,7 @@ * * Available variables * - attributes: A list of HTML attributes for the details element. + * - errors: (optional) Any errors for this details element, may not be set. * - title: (optional) The title of the element, may not be set. * - description: (optional) The description of the element, may not be set. * - children: (optional) The children of the element, may not be set. @@ -20,6 +21,12 @@ {{ title }} {%- endif -%} + {% if errors %} +
+ {{ errors }} +
+ {% endif %} + {{ description }} {{ children }} {{ value }} 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 e4804048f83929124326c3012cc6d54302f5a359..a37990bdc284085218f824392e4fe52188370b0d 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 @@ -402,6 +402,14 @@ form_test.button_class: requirements: _access: 'TRUE' +form_test.details_form: + path: '/form_test/details-form' + defaults: + _form: '\Drupal\form_test\Form\FormTestDetailsForm' + _title: 'Form details form test' + requirements: + _access: 'TRUE' + form_test.description_display: path: '/form_test/form-descriptions' defaults: diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestDetailsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestDetailsForm.php new file mode 100644 index 0000000000000000000000000000000000000000..8944a4cc18ff5116ec11918a5e3fb0915dca03ff --- /dev/null +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestDetailsForm.php @@ -0,0 +1,51 @@ + 'details', + '#title' => 'Details element', + '#open' => TRUE, + ]; + $form['submit'] = ['#type' => 'submit', '#value' => 'Submit']; + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + $form_state->setErrorByName('meta', 'I am an error on the details element.'); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + } + +} diff --git a/core/themes/classy/templates/form/details.html.twig b/core/themes/classy/templates/form/details.html.twig index 9b149d77b29d0e4e1247825946f7e6ccd709c121..a4ce6f63dde0eaca216127dacca5a93de65d81a7 100644 --- a/core/themes/classy/templates/form/details.html.twig +++ b/core/themes/classy/templates/form/details.html.twig @@ -5,6 +5,7 @@ * * Available variables * - attributes: A list of HTML attributes for the details element. + * - errors: (optional) Any errors for this details element, may not be set. * - title: (optional) The title of the element, may not be set. * - description: (optional) The description of the element, may not be set. * - children: (optional) The children of the element, may not be set. @@ -18,6 +19,11 @@ {{ title }} {%- endif -%}
+ {% if errors %} +
+ {{ errors }} +
+ {% endif %} {%- if description -%}
{{ description }}
{%- endif -%}