diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php index a68927a..9d5ad1a 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php @@ -232,6 +232,19 @@ public static function processDatetime(&$element, FormStateInterface $form_state $element['#tree'] = TRUE; + // Visibility and disability 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', 'disabled', 'enabled']); + }, 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) : ''; @@ -269,6 +282,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. @@ -301,6 +315,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 30c938f..615bc63 100644 --- a/core/misc/states.js +++ b/core/misc/states.js @@ -609,13 +609,21 @@ 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, which will + // not be a true