Commit 33da0473 authored by Cottser's avatar Cottser

Issue #2708255 by dawehner, yoroy, swentel, Jacine, Gábor Hojtsy: Add support...

Issue #2708255 by dawehner, yoroy, swentel, Jacine, Gábor Hojtsy: Add support for #required on #type => details
parent 6316f212
...@@ -254,6 +254,7 @@ function template_preprocess_details(&$variables) { ...@@ -254,6 +254,7 @@ function template_preprocess_details(&$variables) {
$variables['description'] = (!empty($element['#description'])) ? $element['#description'] : ''; $variables['description'] = (!empty($element['#description'])) ? $element['#description'] : '';
$variables['children'] = (isset($element['#children'])) ? $element['#children'] : ''; $variables['children'] = (isset($element['#children'])) ? $element['#children'] : '';
$variables['value'] = (isset($element['#value'])) ? $element['#value'] : ''; $variables['value'] = (isset($element['#value'])) ? $element['#value'] : '';
$variables['required'] = !empty($element['#required']) ? $element['#required'] : NULL;
// Suppress error messages. // Suppress error messages.
$variables['errors'] = NULL; $variables['errors'] = NULL;
......
...@@ -137,6 +137,20 @@ function testGroupElements() { ...@@ -137,6 +137,20 @@ function testGroupElements() {
$this->assertTrue(count($elements) == 1); $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.. * Tests a form with a autocomplete setting..
*/ */
......
...@@ -17,8 +17,14 @@ ...@@ -17,8 +17,14 @@
*/ */
#} #}
<details{{ attributes }}> <details{{ attributes }}>
{%
set summary_classes = [
required ? 'js-form-required',
required ? 'form-required',
]
%}
{%- if title -%} {%- if title -%}
<summary{{ summary_attributes }}>{{ title }}</summary> <summary{{ summary_attributes.addClass(summary_classes) }}>{{ title }}</summary>
{%- endif -%} {%- endif -%}
{% if errors %} {% if errors %}
......
...@@ -418,10 +418,11 @@ form_test.description_display: ...@@ -418,10 +418,11 @@ form_test.description_display:
_access: 'TRUE' _access: 'TRUE'
form_test.group_details: form_test.group_details:
path: '/form-test/group-details' path: '/form-test/group-details/{required}'
defaults: defaults:
_form: '\Drupal\form_test\Form\FormTestGroupDetailsForm' _form: '\Drupal\form_test\Form\FormTestGroupDetailsForm'
_title: 'Group details testing' _title: 'Group details testing'
required: FALSE
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
...@@ -434,10 +435,11 @@ form_test.group_container: ...@@ -434,10 +435,11 @@ form_test.group_container:
_access: 'TRUE' _access: 'TRUE'
form_test.group_fieldset: form_test.group_fieldset:
path: '/form-test/group-fieldset' path: '/form-test/group-fieldset/{required}'
defaults: defaults:
_form: '\Drupal\form_test\Form\FormTestGroupFieldsetForm' _form: '\Drupal\form_test\Form\FormTestGroupFieldsetForm'
_title: 'Group fieldset testing' _title: 'Group fieldset testing'
required: FALSE
requirements: requirements:
_access: 'TRUE' _access: 'TRUE'
......
...@@ -20,11 +20,12 @@ public function getFormId() { ...@@ -20,11 +20,12 @@ public function getFormId() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state, $required = FALSE) {
$form['details'] = array( $form['details'] = array(
'#type' => 'details', '#type' => 'details',
'#title' => 'Root element', '#title' => 'Root element',
'#open' => TRUE, '#open' => TRUE,
'#required' => !empty($required),
); );
$form['meta'] = array( $form['meta'] = array(
'#type' => 'details', '#type' => 'details',
......
...@@ -20,10 +20,11 @@ public function getFormId() { ...@@ -20,10 +20,11 @@ public function getFormId() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state, $required = FALSE) {
$form['fieldset'] = array( $form['fieldset'] = array(
'#type' => 'fieldset', '#type' => 'fieldset',
'#title' => 'Fieldset', '#title' => 'Fieldset',
'#required' => !empty($required),
); );
$form['meta'] = array( $form['meta'] = array(
'#type' => 'container', '#type' => 'container',
......
...@@ -16,7 +16,13 @@ ...@@ -16,7 +16,13 @@
#} #}
<details{{ attributes }}> <details{{ attributes }}>
{%- if title -%} {%- 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 -%} {%- endif -%}
<div class="details-wrapper"> <div class="details-wrapper">
{% if errors %} {% if errors %}
......
...@@ -15,8 +15,14 @@ ...@@ -15,8 +15,14 @@
*/ */
#} #}
<details{{ attributes }}> <details{{ attributes }}>
{%
set summary_classes = [
required ? 'js-form-required',
required ? 'form-required',
]
%}
{%- if title -%} {%- if title -%}
<summary{{ summary_attributes }}>{{ title }}</summary> <summary{{ summary_attributes.addClass(summary_classes) }}>{{ title }}</summary>
{%- endif -%} {%- endif -%}
{% if errors %} {% if errors %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment