From 5dafd4dae35208752364095c84661cc52e24e046 Mon Sep 17 00:00:00 2001 From: Gordon Heydon Date: Wed, 3 May 2017 23:51:26 +1000 Subject: [PATCH] 2419131-51 # Conflicts: # core/lib/Drupal/Core/Datetime/Element/Datetime.php # core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php # core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php # core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php # core/modules/system/templates/datetime-wrapper.html.twig # core/themes/classy/templates/form/datetime-wrapper.html.twig # Conflicts: # core/modules/datetime/src/Tests/DateTimeFieldTest.php # core/modules/datetime_range/src/Tests/DateRangeFieldTest.php --- core/includes/theme.inc | 15 ++ core/lib/Drupal/Core/Datetime/Element/Datetime.php | 15 +- core/misc/states.js | 10 + .../Field/FieldWidget/DateTimeWidgetBase.php | 7 + .../datetime/src/Tests/DateTimeFieldTest.php | 228 ++++++++++----------- .../datetime_states/datetime_states.info.yml | 8 + .../datetime_states/datetime_states.routing.yml | 6 + .../modules/datetime_states/src/Form/StateForm.php | 70 +++++++ .../src/Tests/DateRangeFieldTest.php | 86 ++++---- .../system/templates/datetime-wrapper.html.twig | 31 +-- .../templates/form/datetime-wrapper.html.twig | 41 ++-- core/themes/seven/css/components/form.css | 2 + .../templates/form/datetime-wrapper.html.twig | 27 ++- 13 files changed, 339 insertions(+), 207 deletions(-) create mode 100644 core/modules/datetime/tests/modules/datetime_states/datetime_states.info.yml create mode 100644 core/modules/datetime/tests/modules/datetime_states/datetime_states.routing.yml create mode 100644 core/modules/datetime/tests/modules/datetime_states/src/Form/StateForm.php diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 498c490beb..a216170dde 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; @@ -586,6 +594,13 @@ function template_preprocess_datetime_wrapper(&$variables) { $variables['description_attributes'] = new Attribute($description_attributes); } + // 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 d00547535b..f555a16a3c 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 = [ '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) : ''; diff --git a/core/misc/states.js b/core/misc/states.js index 24374b625f..2e183163f9 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 f965e21848..24b521422c 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php @@ -17,6 +17,13 @@ class DateTimeWidgetBase extends WidgetBase { * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { + // We are nesting some sub-elements inside the parent, so we need a wrapper. + // We also need to add another #title attribute at the top level for ease in + // 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['value'] = [ '#type' => 'datetime', '#default_value' => NULL, diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index cb487d6dbf..7deb1a48c6 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -35,7 +35,7 @@ protected function getTestFieldType() { /** * Tests date field functionality. */ - public function testDateField() { + function testDateField() { $field_name = $this->fieldStorage->getName(); // Loop through defined timezones to test that date-only fields work at the @@ -47,10 +47,8 @@ public 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"]//label[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.'); - $this->assertFieldByXPath('//input[@aria-describedby="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA description found'); // Build up a date in the UTC timezone. Note that using this will also // mimic the user in a different timezone simply entering '2012-12-31' via @@ -62,13 +60,13 @@ public function testDateField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date->format($date_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($date->format($date_format)); $this->assertNoRaw($date->format($time_format)); @@ -86,15 +84,15 @@ public function testDateField() { $this->assertEqual('2012-12-31', $entity->{$field_name}->value); // Reset display options since these get changed below. - $this->displayOptions = [ + $this->displayOptions = array( 'type' => 'datetime_default', 'label' => 'hidden', - 'settings' => ['format_type' => 'medium'] + $this->defaultSettings, - ]; + 'settings' => array('format_type' => 'medium') + $this->defaultSettings, + ); // Verify that the date is output according to the formatter settings. - $options = [ - 'format_type' => ['short', 'medium', 'long'], - ]; + $options = array( + 'format_type' => array('short', 'medium', 'long'), + ); // Formats that display a time component for date-only fields will display // the default time, so that is applied before calculating the expected // value. @@ -102,7 +100,7 @@ public function testDateField() { foreach ($options as $setting => $values) { foreach ($values as $new_value) { // Update the entity display settings. - $this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings; + $this->displayOptions['settings'] = array($setting => $new_value) + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); @@ -115,7 +113,7 @@ public function testDateField() { $expected = format_date($date->getTimestamp(), $new_value, '', DATETIME_STORAGE_TIMEZONE); $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE); $this->renderTestEntity($id); - $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso])); + $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso))); break; } } @@ -129,17 +127,17 @@ public function testDateField() { ->save(); $expected = $date->format(DATETIME_DATE_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_custom' formatter works. $this->displayOptions['type'] = 'datetime_custom'; - $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings; + $this->displayOptions['settings'] = array('date_format' => 'm/d/Y') + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); $expected = $date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_time_ago' formatter works for intervals in the // past. First update the test entity so that the date difference always @@ -154,11 +152,11 @@ public function testDateField() { $entity->save(); $this->displayOptions['type'] = 'datetime_time_ago'; - $this->displayOptions['settings'] = [ + $this->displayOptions['settings'] = array( 'future_format' => '@interval in the future', 'past_format' => '@interval in the past', 'granularity' => 3, - ]; + ); entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); @@ -166,7 +164,7 @@ public function testDateField() { '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_time_ago' formatter works for intervals in the // future. First update the test entity so that the date difference always @@ -187,14 +185,14 @@ public function testDateField() { '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); } } /** * Tests date and time field. */ - public function testDatetimeField() { + function testDatetimeField() { $field_name = $this->fieldStorage->getName(); // Change the field to a datetime field. $this->fieldStorage->setSetting('datetime_type', 'datetime'); @@ -204,9 +202,6 @@ public function testDatetimeField() { $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.'); - $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); - $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found'); // Build up a date in the UTC timezone. $value = '2012-12-31 00:00:00'; @@ -219,25 +214,25 @@ public function testDatetimeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date->format($date_format), "{$field_name}[0][value][time]" => $date->format($time_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($date->format($date_format)); $this->assertRaw($date->format($time_format)); // Verify that the date is output according to the formatter settings. - $options = [ - 'format_type' => ['short', 'medium', 'long'], - ]; + $options = array( + 'format_type' => array('short', 'medium', 'long'), + ); foreach ($options as $setting => $values) { foreach ($values as $new_value) { // Update the entity display settings. - $this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings; + $this->displayOptions['settings'] = array($setting => $new_value) + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); @@ -249,7 +244,7 @@ public function testDatetimeField() { $expected = format_date($date->getTimestamp(), $new_value); $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $this->renderTestEntity($id); - $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso])); + $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso))); break; } } @@ -263,27 +258,27 @@ public function testDatetimeField() { ->save(); $expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_custom' formatter works. $this->displayOptions['type'] = 'datetime_custom'; - $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings; + $this->displayOptions['settings'] = array('date_format' => 'm/d/Y g:i:s A') + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); $expected = $date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'timezone_override' setting works. $this->displayOptions['type'] = 'datetime_custom'; - $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings; + $this->displayOptions['settings'] = array('date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York') + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); - $expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); + $expected = $date->format($this->displayOptions['settings']['date_format'], array('timezone' => 'America/New_York')); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_time_ago' formatter works for intervals in the // past. First update the test entity so that the date difference always @@ -298,11 +293,11 @@ public function testDatetimeField() { $entity->save(); $this->displayOptions['type'] = 'datetime_time_ago'; - $this->displayOptions['settings'] = [ + $this->displayOptions['settings'] = array( 'future_format' => '@interval from now', 'past_format' => '@interval earlier', 'granularity' => 3, - ]; + ); entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); @@ -310,7 +305,7 @@ public function testDatetimeField() { '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_time_ago' formatter works for intervals in the // future. First update the test entity so that the date difference always @@ -331,13 +326,13 @@ public function testDatetimeField() { '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); - $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); } /** * Tests Date List Widget functionality. */ - public function testDatelistWidget() { + function testDatelistWidget() { $field_name = $this->fieldStorage->getName(); // Ensure field is set to a date only field. @@ -346,20 +341,17 @@ public function testDatelistWidget() { // Change the widget to a datelist widget. entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default') - ->setComponent($field_name, [ + ->setComponent($field_name, array( 'type' => 'datetime_datelist', - 'settings' => [ + 'settings' => array( 'date_order' => 'YMD', - ], - ]) + ), + )) ->save(); \Drupal::entityManager()->clearCachedFieldDefinitions(); // Display creation form. $this->drupalGet('entity_test/add'); - $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); - $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found'); // Assert that Hour and Minute Elements do not appear on Date Only $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element not found on Date Only.'); @@ -370,7 +362,7 @@ public function testDatelistWidget() { $this->drupalGet($fieldEditUrl); // Click on the widget settings button to open the widget settings form. - $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); + $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit"); $xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]"; $this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.'); @@ -380,14 +372,14 @@ public function testDatelistWidget() { // Change the widget to a datelist widget. entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default') - ->setComponent($field_name, [ + ->setComponent($field_name, array( 'type' => 'datetime_datelist', - 'settings' => [ + 'settings' => array( 'increment' => 1, 'date_order' => 'YMD', 'time_type' => '12', - ], - ]) + ), + )) ->save(); \Drupal::entityManager()->clearCachedFieldDefinitions(); @@ -396,7 +388,7 @@ public function testDatelistWidget() { $this->drupalGet($fieldEditUrl); // Click on the widget settings button to open the widget settings form. - $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); + $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit"); $this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.'); // Display creation form. @@ -423,9 +415,9 @@ public function testDatelistWidget() { $this->assertOptionByText("edit-$field_name-0-value-ampm", t('AM/PM')); // Submit a valid date and ensure it is accepted. - $date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15]; + $date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15); - $edit = []; + $edit = array(); // Add the ampm indicator since we are testing 12 hour time. $date_value['ampm'] = 'am'; foreach ($date_value as $part => $value) { @@ -435,7 +427,7 @@ public function testDatelistWidget() { $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.'); $this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.'); @@ -446,14 +438,14 @@ public function testDatelistWidget() { // Test the widget using increment other than 1 and 24 hour mode. entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default') - ->setComponent($field_name, [ + ->setComponent($field_name, array( 'type' => 'datetime_datelist', - 'settings' => [ + 'settings' => array( 'increment' => 15, 'date_order' => 'YMD', 'time_type' => '24', - ], - ]) + ), + )) ->save(); \Drupal::entityManager()->clearCachedFieldDefinitions(); @@ -466,9 +458,9 @@ public function testDatelistWidget() { $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-ampm\"]", NULL, 'AMPM element not found.'); // Submit a valid date and ensure it is accepted. - $date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15]; + $date_value = array('year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15); - $edit = []; + $edit = array(); foreach ($date_value as $part => $value) { $edit["{$field_name}[0][value][$part]"] = $value; } @@ -476,7 +468,7 @@ public function testDatelistWidget() { $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.'); $this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.'); @@ -486,14 +478,14 @@ public function testDatelistWidget() { // Test the widget for partial completion of fields. entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default') - ->setComponent($field_name, [ + ->setComponent($field_name, array( 'type' => 'datetime_datelist', - 'settings' => [ + 'settings' => array( 'increment' => 1, 'date_order' => 'YMD', 'time_type' => '24', - ], - ]) + ), + )) ->save(); \Drupal::entityManager()->clearCachedFieldDefinitions(); @@ -505,7 +497,7 @@ public function testDatelistWidget() { $this->drupalGet('entity_test/add'); // Submit a partial date and ensure and error message is provided. - $edit = []; + $edit = array(); foreach ($date_value as $part => $value) { $edit["{$field_name}[0][value][$part]"] = $value; } @@ -520,8 +512,8 @@ public function testDatelistWidget() { // Test the widget for complete input with zeros as part of selections. $this->drupalGet('entity_test/add'); - $date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0']; - $edit = []; + $date_value = array('year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0'); + $edit = array(); foreach ($date_value as $part => $value) { $edit["{$field_name}[0][value][$part]"] = $value; } @@ -530,13 +522,13 @@ public function testDatelistWidget() { $this->assertResponse(200); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); // Test the widget to ensure zeros are not deselected on validation. $this->drupalGet('entity_test/add'); - $date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0']; - $edit = []; + $date_value = array('year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0'); + $edit = array(); foreach ($date_value as $part => $value) { $edit["{$field_name}[0][value][$part]"] = $value; } @@ -582,18 +574,18 @@ protected function datelistDataProvider() { /** * Test default value functionality. */ - public function testDefaultValue() { + function testDefaultValue() { // Create a test content type. - $this->drupalCreateContentType(['type' => 'date_content']); + $this->drupalCreateContentType(array('type' => 'date_content')); // Create a field storage with settings to validate. $field_name = Unicode::strtolower($this->randomMachineName()); - $field_storage = FieldStorageConfig::create([ + $field_storage = FieldStorageConfig::create(array( 'field_name' => $field_name, 'entity_type' => 'node', 'type' => 'datetime', - 'settings' => ['datetime_type' => 'date'], - ]); + 'settings' => array('datetime_type' => 'date'), + )); $field_storage->save(); $field = FieldConfig::create([ @@ -609,9 +601,9 @@ public function testDefaultValue() { $this->setSiteTimezone($timezone); // Set now as default_value. - $field_edit = [ + $field_edit = array( 'default_value_input[default_date_type]' => 'now', - ]; + ); $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings')); // Check that default value is selected in default value form. @@ -622,10 +614,10 @@ public function testDefaultValue() { // Check if default_date has been stored successfully. $config_entity = $this->config('field.field.node.date_content.' . $field_name) ->get(); - $this->assertEqual($config_entity['default_value'][0], [ + $this->assertEqual($config_entity['default_value'][0], array( 'default_date_type' => 'now', 'default_date' => 'now', - ], 'Default value has been stored successfully'); + ), 'Default value has been stored successfully'); // Clear field cache in order to avoid stale cache values. \Drupal::entityManager()->clearCachedFieldDefinitions(); @@ -637,19 +629,19 @@ public function testDefaultValue() { ->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT)); // Set an invalid relative default_value to test validation. - $field_edit = [ + $field_edit = array( 'default_value_input[default_date_type]' => 'relative', 'default_value_input[default_date]' => 'invalid date', - ]; + ); $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings')); $this->assertText('The relative date value entered is invalid.'); // Set a relative default_value. - $field_edit = [ + $field_edit = array( 'default_value_input[default_date_type]' => 'relative', 'default_value_input[default_date]' => '+90 days', - ]; + ); $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings')); // Check that default value is selected in default value form. @@ -660,10 +652,10 @@ public function testDefaultValue() { // Check if default_date has been stored successfully. $config_entity = $this->config('field.field.node.date_content.' . $field_name) ->get(); - $this->assertEqual($config_entity['default_value'][0], [ + $this->assertEqual($config_entity['default_value'][0], array( 'default_date_type' => 'relative', 'default_date' => '+90 days', - ], 'Default value has been stored successfully'); + ), 'Default value has been stored successfully'); // Clear field cache in order to avoid stale cache values. \Drupal::entityManager()->clearCachedFieldDefinitions(); @@ -676,9 +668,9 @@ public function testDefaultValue() { ->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT)); // Remove default value. - $field_edit = [ + $field_edit = array( 'default_value_input[default_date_type]' => '', - ]; + ); $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings')); // Check that default value is selected in default value form. @@ -704,7 +696,7 @@ public function testDefaultValue() { /** * Test that invalid values are caught and marked as invalid. */ - public function testInvalidField() { + function testInvalidField() { // Change the field to a datetime field. $this->fieldStorage->setSetting('datetime_type', 'datetime'); $this->fieldStorage->save(); @@ -717,72 +709,72 @@ public function testInvalidField() { // Submit invalid dates and ensure they is not accepted. $date_value = ''; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '12:00:00', - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', 'Empty date value has been caught.'); $date_value = 'aaaa-12-01'; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00', - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', ['%date' => $date_value])); + $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', array('%date' => $date_value))); $date_value = '2012-75-01'; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00', - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', ['%date' => $date_value])); + $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', array('%date' => $date_value))); $date_value = '2012-12-99'; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => '00:00:00', - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', ['%date' => $date_value])); + $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', array('%date' => $date_value))); $date_value = '2012-12-01'; $time_value = ''; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value, - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); $this->assertText('date is invalid', 'Empty time value has been caught.'); $date_value = '2012-12-01'; $time_value = '49:00:00'; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value, - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', ['%time' => $time_value])); + $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', array('%time' => $time_value))); $date_value = '2012-12-01'; $time_value = '12:99:00'; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value, - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', ['%time' => $time_value])); + $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', array('%time' => $time_value))); $date_value = '2012-12-01'; $time_value = '12:15:99'; - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $date_value, "{$field_name}[0][value][time]" => $time_value, - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); - $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', ['%time' => $time_value])); + $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', array('%time' => $time_value))); } /** 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 0000000000..75e139ff87 --- /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 0000000000..f5e44fe978 --- /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 0000000000..9b00a6dbf4 --- /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/datetime_range/src/Tests/DateRangeFieldTest.php b/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php index bf75fc0f1f..d8054990a9 100644 --- a/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php +++ b/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php @@ -61,12 +61,9 @@ public function testDateRangeField() { $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Start date element found.'); $this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.'); - $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[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]", '', 'Start time element not found.'); $this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.'); - $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); - $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found'); // Build up dates in the UTC timezone. $value = '2012-12-31 00:00:00'; @@ -78,14 +75,14 @@ public function testDateRangeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $end_date->format($date_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($start_date->format($date_format)); $this->assertNoRaw($start_date->format($time_format)); $this->assertRaw($end_date->format($date_format)); @@ -156,17 +153,17 @@ public function testDateRangeField() { ->save(); $expected = $start_date->format(DATETIME_DATE_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATE_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); // Verify that the custom formatter works. $this->displayOptions['type'] = 'daterange_custom'; - $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings; + $this->displayOptions['settings'] = array('date_format' => 'm/d/Y') + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); // Test formatters when start date and end date are the same $this->drupalGet('entity_test/add'); @@ -176,15 +173,15 @@ public function testDateRangeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $start_date->format($date_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); datetime_date_default_time($start_date); @@ -223,17 +220,17 @@ public function testDateRangeField() { ->save(); $expected = $start_date->format(DATETIME_DATE_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); $this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->displayOptions['type'] = 'daterange_custom'; - $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings; + $this->displayOptions['settings'] = array('date_format' => 'm/d/Y') + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); $expected = $start_date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); $this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); } } @@ -254,9 +251,6 @@ public function testDatetimeRangeField() { $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Start time element found.'); $this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.'); $this->assertFieldByName("{$field_name}[0][end_value][time]", '', 'End time element found.'); - $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); - $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found'); // Build up dates in the UTC timezone. $value = '2012-12-31 00:00:00'; @@ -272,16 +266,16 @@ public function testDatetimeRangeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][value][time]" => $start_date->format($time_format), "{$field_name}[0][end_value][date]" => $end_date->format($date_format), "{$field_name}[0][end_value][time]" => $end_date->format($time_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($start_date->format($date_format)); $this->assertRaw($start_date->format($time_format)); $this->assertRaw($end_date->format($date_format)); @@ -318,7 +312,7 @@ public function testDatetimeRangeField() { ->save(); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'datetime_custom' formatter works. $this->displayOptions['type'] = 'daterange_custom'; @@ -328,7 +322,7 @@ public function testDatetimeRangeField() { ->save(); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'timezone_override' setting works. $this->displayOptions['type'] = 'daterange_custom'; @@ -339,7 +333,7 @@ public function testDatetimeRangeField() { $expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); // Test formatters when start date and end date are the same $this->drupalGet('entity_test/add'); @@ -350,17 +344,17 @@ public function testDatetimeRangeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][value][time]" => $start_date->format($time_format), "{$field_name}[0][end_value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][time]" => $start_date->format($time_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->displayOptions = [ 'type' => 'daterange_default', @@ -393,7 +387,7 @@ public function testDatetimeRangeField() { ->save(); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); $this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->displayOptions['type'] = 'daterange_custom'; @@ -403,7 +397,7 @@ public function testDatetimeRangeField() { ->save(); $expected = $start_date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); $this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); } @@ -421,12 +415,9 @@ public function testAlldayRangeField() { $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Start date element found.'); $this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.'); - $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[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]", '', 'Start time element not found.'); $this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.'); - $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); - $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found'); // Build up dates in the proper timezone. $value = '2012-12-31 00:00:00'; @@ -438,14 +429,14 @@ public function testAlldayRangeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $end_date->format($date_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->assertRaw($start_date->format($date_format)); $this->assertNoRaw($start_date->format($time_format)); $this->assertRaw($end_date->format($date_format)); @@ -482,17 +473,17 @@ public function testAlldayRangeField() { ->save(); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); // Verify that the custom formatter works. $this->displayOptions['type'] = 'daterange_custom'; - $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings; + $this->displayOptions['settings'] = array('date_format' => 'm/d/Y') + $this->defaultSettings; entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') ->setComponent($field_name, $this->displayOptions) ->save(); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); // Verify that the 'timezone_override' setting works. $this->displayOptions['type'] = 'daterange_custom'; @@ -503,7 +494,7 @@ public function testAlldayRangeField() { $expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); // Test formatters when start date and end date are the same $this->drupalGet('entity_test/add'); @@ -516,14 +507,14 @@ public function testAlldayRangeField() { $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); - $edit = [ + $edit = array( "{$field_name}[0][value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $start_date->format($date_format), - ]; + ); $this->drupalPostForm(NULL, $edit, t('Save')); preg_match('|entity_test/manage/(\d+)|', $this->url, $match); $id = $match[1]; - $this->assertText(t('entity_test @id has been created.', ['@id' => $id])); + $this->assertText(t('entity_test @id has been created.', array('@id' => $id))); $this->displayOptions = [ 'type' => 'daterange_default', @@ -558,7 +549,7 @@ public function testAlldayRangeField() { ->save(); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' THESEPARATOR ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', array('%expected' => $expected))); $this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->displayOptions['type'] = 'daterange_custom'; @@ -568,7 +559,7 @@ public function testAlldayRangeField() { ->save(); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' THESEPARATOR ' . $end_date->format($this->displayOptions['settings']['date_format']); $this->renderTestEntity($id); - $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected])); + $this->assertText($expected, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', array('%expected' => $expected))); $this->assertText(' THESEPARATOR ', 'Found proper separator'); } @@ -596,9 +587,6 @@ public function testDatelistWidget() { // Display creation form. $this->drupalGet('entity_test/add'); - $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); - $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found'); - $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found'); // Assert that Hour and Minute Elements do not appear on Date Only. $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element not found on Date Only.'); diff --git a/core/modules/system/templates/datetime-wrapper.html.twig b/core/modules/system/templates/datetime-wrapper.html.twig index a14da8937b..d822ed9812 100644 --- a/core/modules/system/templates/datetime-wrapper.html.twig +++ b/core/modules/system/templates/datetime-wrapper.html.twig @@ -16,22 +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 %} -{% if description %} - - {{ description }} - -{% endif %} + + {% 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 5b52f2daa3..069f918b65 100644 --- a/core/themes/classy/templates/form/datetime-wrapper.html.twig +++ b/core/themes/classy/templates/form/datetime-wrapper.html.twig @@ -14,23 +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 f3acc2c0ba..213c1b800b 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 3c37ffd20d..29ed9a3853 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 }} + -- 2.11.0 (Apple Git-81)