Problem/Motivation

If you code a datetime element like

$form['datetime'] = [
  '#type' => 'datetime',
  '#title' => t('Downtime DateTime'),
];

You'll get a PHP warning.

Proposed resolution

Fix code so that #default_value is not expected to be set.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Comments

shahgm created an issue. See original summary.

cilefen’s picture

Component: other » base system
Priority: Major » Normal
Status: Needs review » Active
Issue tags: -core, -datetime

Please update the issue summary with the steps needed to reproduce the bug. We must understand the conditions that cause the notice. Please use the issue template.

elandirayan’s picture

Assigned: Unassigned » elandirayan
Status: Active » Patch (to be ported)
StatusFileSize
new683 bytes

Adding the Patch for the fix.

elandirayan’s picture

Status: Patch (to be ported) » Fixed
cilefen’s picture

Version: 8.0.3 » 8.0.x-dev
Status: Fixed » Needs review

Thank you for posting this patch, however you are using the wrong status. We still need the issue summary updated it to understand what situation causes this error.

shahgm’s picture

I reset my code by removing fix and not passing default value from my custom code as before to reproduce the error. However, i couldn't reproduce the error. I am sure it's a good idea to check if default_value index is set or not before accessing it's value. Right? I don't know if there is process before this checking for it or not, but this valueCallback function is not checking for sure.

Thanks,
Shah.

shahgm’s picture

oh i show it... form loads fine, but if you go to error log it shows error right there... So, to reproduce you do is create form field without specifying default value in it...

$form['datetime'] = array(
'#type' => 'datetime',
'#title' => t('Downtime DateTime'),
);

and check error log ...

Thanks,
Shah

elandirayan’s picture

Hi,

I have contacted Shah, who created bug and updated info and tested with the patch and it is working fine. please review and mark it as fixed.

Thanks
Elan

elandirayan’s picture

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

rijidij’s picture

This issue is over 2 years old. Will it be committed to core?
I've re-rolled the patch for 8.4.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ethomas08’s picture

I tested the patch in #14 on a clean install of Drupal 8.6 and it works. No more warning showing up in the logs after applying the patch.

amateescu’s picture

Status: Needs review » Needs work
Issue tags: +Novice

The patch looks good, we just need to fix one thing before it can be committed:

+++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php
@@ -96,7 +96,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
+      $date = isset($element['#default_value']) ? $element['#default_value'] : null;

According to our coding standards, null needs to be upper case.

krzysztof domański’s picture

Assigned: elandirayan » Unassigned
Status: Needs work » Needs review
StatusFileSize
new682 bytes
amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Perfect :)

larowlan’s picture

Issue tags: +Needs tests

Sorry, if this is a bug and we're fixing a regression, we need a test

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I agree with @larowlan we need a test.

ilya.no’s picture

Status: Needs work » Needs review
StatusFileSize
new3.37 KB
new2.21 KB

Attaching patch with test.

Status: Needs review » Needs work

The last submitted patch, 23: datetime-undefined-default-index-2672950-23.patch, failed testing. View results

ilya.no’s picture

Status: Needs work » Needs review
StatusFileSize
new3.37 KB
new413 bytes

Fixed typo.

amateescu’s picture

Status: Needs review » Needs work
+++ b/core/modules/datetime/src/Tests/Form/DateDefaultValueTest.php
@@ -0,0 +1,32 @@
+class DateDefaultValueTest extends WebTestBase {

The test case should be added to the existing \Drupal\KernelTests\Core\Datetime\DatetimeElementFormTest test class.

vj’s picture

Status: Needs work » Needs review
StatusFileSize
new678 bytes
new1.32 KB

Please review and let me know if any changes required.

Status: Needs review » Needs work

The last submitted patch, 27: 2672950-add-condition.patch, failed testing. View results

vj’s picture

Version: 8.6.x-dev » 8.7.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.49 KB
new848 bytes

Status: Needs review » Needs work

The last submitted patch, 29: 2672950-fail-29.patch, failed testing. View results

amateescu’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests

Looks great!

krzysztof domański’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.55 KB
new683 bytes

I think we should add a comment describing what we are testing in this form element. In the future, we will not know for what purpose this element has been added.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Agreed, that comment is very useful.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Crediting @larowlan and @amateescu for patch review. Crediting @shahgm for creating the issue.

Committed ea33b15 and pushed to 8.7.x. Thanks!

Doesn't apply to 8.6.x so have only fixed 8.7.x. If someone wants to backport then this would be eligible.

alexpott’s picture

Issue summary: View changes

  • alexpott committed ea33b15 on 8.7.x
    Issue #2672950 by Vj, ilya.no, Krzysztof Domański, elandirayan, Rijidij...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

dww’s picture

Version: 8.7.x-dev » 8.6.x-dev
Status: Closed (fixed) » Postponed

Found this via #2419131-93: [PP-1] #states attribute does not work on #type datetime

Backporting this would be trivial if #2992580: Custom callbacks doesn't work was also in 8.6.x, since that's where the test class this patch is extending was first added.

With 2992580-09.patch applied to 8.6.x, this patch applies (with minor fuzz).

Can we get #2992580 in and then I'll upload a backport here? I queued a re-test of that against 8.6.x, and I'll RTBC once the bot is happy.

Thanks!
-Derek

dww’s picture

Status: Postponed » Reviewed & tested by the community

Yay, @alexpott just committed #2992580: Custom callbacks doesn't work to 8.6.x, and now #32 applies cleanly, and core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php passes locally.

RTBC.

Thanks!
-Derek

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 32: core-2672950-32.patch, failed testing. View results

dww’s picture

Status: Needs work » Reviewed & tested by the community

Bot is confused. #32 is all green for 8.6.x.

dww’s picture

StatusFileSize
new1.55 KB

According to @drumm in Slack, the bot will continue to be confused until a new file is uploaded in this case. :/ So here's #32 again, so we don't keep getting this marked 'needs work' each day it isn't backported.

  • alexpott committed 358d3d6 on 8.6.x
    Issue #2672950 by Vj, ilya.no, Krzysztof Domański, elandirayan, Rijidij...
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Cherry-picked the 8.7.x back to 8.6.x

dww’s picture

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.