diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 9b89250..fe548e9 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -573,6 +573,14 @@ function template_preprocess_datetime_wrapper(&$variables) { $variables['title'] = $element['#title']; } + // Pass elements #type and #name to template. + if (!empty($element['#type'])) { + $variables['type'] = $element['#type']; + } + if (!empty($element['#name'])) { + $variables['name'] = $element['#name']; + } + // Suppress error messages. $variables['errors'] = NULL; @@ -580,6 +588,13 @@ function template_preprocess_datetime_wrapper(&$variables) { $variables['description'] = $element['#description']; } + // For disabled datetime fields, the 'disabled' attribute should not be + // applied to the wrapper, but the 'form-disabled' class should be. + $variables['disabled'] = !empty($element['#attributes']['disabled']) ? $element['#attributes']['disabled'] : NULL; + if (isset($variables['attributes']['disabled'])) { + unset($variables['attributes']['disabled']); + } + $variables['required'] = FALSE; // For required datetime fields 'form-required' & 'js-form-required' classes // are appended to the label attributes. diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php index 44a3949..b5d0ac7 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php @@ -95,7 +95,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form ); } else { - $date = $element['#default_value']; + $date = isset($element['#default_value']) ? $element['#default_value'] : NULL; if ($date instanceof DrupalDateTime && !$date->hasErrors()) { $input = array( 'date' => $date->format($element['#date_date_format']), @@ -227,6 +227,19 @@ public static function processDatetime(&$element, FormStateInterface $form_state $element['#tree'] = TRUE; + // Visibility states need to be applied to the element as a whole, but the + // other states apply to the individual parts. + if (isset($element['#states']) && is_array($element['#states'])) { + $wrapper_states = array_filter($element['#states'], function ($key) { + return in_array($key, ['visible', 'invisible']); + }, ARRAY_FILTER_USE_KEY); + $children_states = array_diff_key($element['#states'], $wrapper_states); + } + else { + $wrapper_states = $children_states = []; + } + $element['#states'] = $wrapper_states; + if ($element['#date_date_element'] != 'none') { $date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : ''; @@ -264,6 +277,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state '#size' => max(12, strlen($element['#value']['date'])), '#error_no_message' => TRUE, '#date_date_format' => $element['#date_date_format'], + '#states' => $children_states, ); // Allows custom callbacks to alter the element. @@ -296,6 +310,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state '#required' => $element['#required'], '#size' => 12, '#error_no_message' => TRUE, + '#states' => $children_states, ); // Allows custom callbacks to alter the element. diff --git a/core/misc/states.js b/core/misc/states.js index 24374b6..2e18316 100644 --- a/core/misc/states.js +++ b/core/misc/states.js @@ -610,6 +610,9 @@ .prop('disabled', e.value) .closest('.js-form-item, .js-form-submit, .js-form-wrapper').toggleClass('form-disabled', e.value) .find('select, input, textarea').prop('disabled', e.value); + // A complex form element, like 'datetime' (one made up of multiple + // sub-elements) may have a label for the element as a whole. + $(e.target).closest('.js-complex-form-item').toggleClass('form-disabled', e.value); // Note: WebKit nightlies don't reflect that change correctly. // See https://bugs.webkit.org/show_bug.cgi?id=23789 @@ -621,13 +624,20 @@ if (e.value) { var label = 'label' + (e.target.id ? '[for=' + e.target.id + ']' : ''); var $label = $(e.target).attr({'required': 'required', 'aria-required': 'aria-required'}).closest('.js-form-item, .js-form-wrapper').find(label); + // A complex form element, like 'datetime' (one made up of multiple + // sub-elements) may have a label for the element as a whole. + var $complexLabel = $(e.target).closest('.js-complex-form-item').children('label'); // Avoids duplicate required markers on initialization. if (!$label.hasClass('js-form-required').length) { $label.addClass('js-form-required form-required'); } + if (!$complexLabel.hasClass('js-form-required').length) { + $complexLabel.addClass('js-form-required form-required'); + } } else { $(e.target).removeAttr('required aria-required').closest('.js-form-item, .js-form-wrapper').find('label.js-form-required').removeClass('js-form-required form-required'); + $(e.target).closest('.js-complex-form-item').children('label').removeClass('js-form-required form-required'); } } }); diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php index aa6175a..fa1d196 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php @@ -22,9 +22,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // identifying this item in error messages. We do not want to display this // title because the actual title display is handled at a higher level by // the Field module. - $element['#theme_wrappers'][] = 'datetime_wrapper'; - $element['#attributes']['class'][] = 'container-inline'; $element['value'] = array( '#type' => 'datetime', diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index f7aeae8..7deb1a4 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -47,7 +47,7 @@ function testDateField() { // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); - $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]/h4[contains(@class, "js-form-required")]', TRUE, 'Required markup found'); + $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]/div/label[contains(@class, "js-form-required")]', TRUE, 'Required markup found'); $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Time element not found.'); // Build up a date in the UTC timezone. Note that using this will also diff --git a/core/modules/datetime/tests/modules/datetime_states/datetime_states.info.yml b/core/modules/datetime/tests/modules/datetime_states/datetime_states.info.yml new file mode 100644 index 0000000..75e139f --- /dev/null +++ b/core/modules/datetime/tests/modules/datetime_states/datetime_states.info.yml @@ -0,0 +1,8 @@ +name: 'Datetime #states test' +type: module +description: 'Provides an example demonstrating how form #states affect datetime elements.' +package: Testing +version: VERSION +core: 8.x +dependencies: + - datetime diff --git a/core/modules/datetime/tests/modules/datetime_states/datetime_states.routing.yml b/core/modules/datetime/tests/modules/datetime_states/datetime_states.routing.yml new file mode 100644 index 0000000..f5e44fe --- /dev/null +++ b/core/modules/datetime/tests/modules/datetime_states/datetime_states.routing.yml @@ -0,0 +1,6 @@ +datetime_states.example: + path: '/datetime/states-example' + defaults: + _form: '\Drupal\datetime_states\Form\StateForm' + requirements: + _access: 'TRUE' diff --git a/core/modules/datetime/tests/modules/datetime_states/src/Form/StateForm.php b/core/modules/datetime/tests/modules/datetime_states/src/Form/StateForm.php new file mode 100644 index 0000000..9b00a6d --- /dev/null +++ b/core/modules/datetime/tests/modules/datetime_states/src/Form/StateForm.php @@ -0,0 +1,70 @@ + 'checkbox', + '#title' => $this->t('Invisible'), + ); + $form['toggle_disabled'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Disabled'), + ); + $form['toggle_required'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Required'), + ); + + + $form['date'] = array( + '#type' => 'datetime', + '#title' => $this->t('Datetime'), + '#description' => $this->t('A datetime form element.'), + '#states' => [ + 'invisible' => [ + ':input[name="toggle_invisible"]' => ['checked' => TRUE], + ], + 'disabled' => [ + ':input[name="toggle_disabled"]' => ['checked' => TRUE], + ], + 'required' => [ + ':input[name="toggle_required"]' => ['checked' => TRUE], + ], + ], + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => $this->t('Save'), + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + + } + +} diff --git a/core/modules/system/templates/datetime-wrapper.html.twig b/core/modules/system/templates/datetime-wrapper.html.twig index 8430baa..d822ed9 100644 --- a/core/modules/system/templates/datetime-wrapper.html.twig +++ b/core/modules/system/templates/datetime-wrapper.html.twig @@ -16,18 +16,25 @@ */ #} {% + set container_classes = [ + 'js-complex-form-item', + ] +%} +{% set title_classes = [ required ? 'js-form-required', required ? 'form-required', ] %} -{% if title %} - {{ title }} -{% endif %} -{{ content }} -{% if errors %} -
- {{ errors }} -
-{% endif %} -{{ description }} + + {% if title %} + {{ title }} + {% endif %} + {{ content }} + {% if errors %} +
+ {{ errors }} +
+ {% endif %} + {{ description }} + diff --git a/core/themes/classy/templates/form/datetime-wrapper.html.twig b/core/themes/classy/templates/form/datetime-wrapper.html.twig index 3f6aa59..069f918 100644 --- a/core/themes/classy/templates/form/datetime-wrapper.html.twig +++ b/core/themes/classy/templates/form/datetime-wrapper.html.twig @@ -14,21 +14,34 @@ */ #} {% + set container_classes = [ + 'js-form-item', + 'form-item', + 'js-form-type-' ~ type|clean_class, + 'form-type-' ~ type|clean_class, + 'js-form-item-' ~ name|clean_class, + 'form-item-' ~ name|clean_class, + 'js-complex-form-item', + disabled == 'disabled' ? 'form-disabled', + ] +%} +{% set title_classes = [ - 'label', required ? 'js-form-required', required ? 'form-required', ] %} -{% if title %} - {{ title }} -{% endif %} -{{ content }} -{% if errors %} -
- {{ errors }} -
-{% endif %} -{% if description %} -
{{ description }}
-{% endif %} + + {% if title %} + {{ title }} + {% endif %} + {{ content }} + {% if errors %} +
+ {{ errors }} +
+ {% endif %} + {% if description %} +
{{ description }}
+ {% endif %} + diff --git a/core/themes/seven/css/components/form.css b/core/themes/seven/css/components/form.css index f3acc2c..213c1b8 100644 --- a/core/themes/seven/css/components/form.css +++ b/core/themes/seven/css/components/form.css @@ -81,6 +81,8 @@ label[for] { .form-disabled input.form-number, .form-disabled input.form-color, .form-disabled input.form-file, +.form-disabled input.form-date, +.form-disabled input.form-time, .form-disabled textarea.form-textarea, .form-disabled select.form-select { border-color: #d4d4d4; diff --git a/core/themes/stable/templates/form/datetime-wrapper.html.twig b/core/themes/stable/templates/form/datetime-wrapper.html.twig index 3c37ffd..29ed9a3 100644 --- a/core/themes/stable/templates/form/datetime-wrapper.html.twig +++ b/core/themes/stable/templates/form/datetime-wrapper.html.twig @@ -14,18 +14,25 @@ */ #} {% + set container_classes = [ + 'js-complex-form-item', + ] +%} +{% set title_classes = [ required ? 'js-form-required', required ? 'form-required', ] %} -{% if title %} - {{ title }} -{% endif %} -{{ content }} -{% if errors %} -
- {{ errors }} -
-{% endif %} -{{ description }} + + {% if title %} + {{ title }} + {% endif %} + {{ content }} + {% if errors %} +
+ {{ errors }} +
+ {% endif %} + {{ description }} +