Problem/Motivation

datetime.module declares global constants, which are only available when the module is enabled. This can cause problems in some scenarios in the install/uninstall process.

Proposed resolution

The constants are directly related to the field item, so create DateTimeItemInterface and duplicate the constants there.

Updates usages in code.

Deprecate the module globals.

Remaining tasks

Create DateTimeItemInterface

Move global constants from datetime.module to new interface (see also #2807785: Move global constants from *.module files into interfaces for how to handle b/c).

Update global constant usages to use the ones on the interface.

User interface changes

N/A

API changes

Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface has been created. There should be no impact on contrib, as field items that extend DateTimeItem or DateRangeItem will inherit the constants.

Global constants in datetime.module are deprecated.

Data model changes

None. The constant values are the same.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mpdonadio created an issue. See original summary.

mpdonadio’s picture

Title: Create DateTimeItemInterface » Create DateTimeItemInterface and DateRangeItemInterface
Issue summary: View changes
Status: Active » Needs review
Issue tags: +Needs change record
FileSize
40.9 KB

Ok, first pass at changes. Didn't move constants from DateTimeItem yet. We will also need a DateRangeItemInterface and move that constant.

Status: Needs review » Needs work

The last submitted patch, 2: 2826404-02.patch, failed testing.

mpdonadio’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
41.12 KB
3.01 KB

This should fix the current fails, but there is still work todo. For reference, from @klausi on #2807785-22: Move global constants from *.module files into interfaces

We cannot use the class in the global scope because the class loader for the module is not there when the module is installed/uninstalled.

You need to leave the hard coded values there unmodified.

mpdonadio’s picture

I started to move the constants from the items classes to the interfaces, and update usages. Since DateRageItem extends DateTimeItem, the refactoring looks a little weird once you swap out the usages. I am thinking that we keep the scope at #4 for now to keep the parent issue moving, and defer further work.

Thoughts?

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

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

jhedstrom’s picture

Status: Needs review » Needs work

I some how missed this. It needs a bit of work to catch up to the latest changes in core.

jofitz’s picture

Status: Needs work » Needs review
FileSize
41.92 KB

Patch in #4 no longer applies. Re-rolled.

mpdonadio’s picture

Updated deprecation messages and found a few more usages. PhpStorm tells me they are all updated. Drafting the CR now.

mpdonadio’s picture

Title: Create DateTimeItemInterface and DateRangeItemInterface » Create DateTimeItemInterface and deprecate global constants in datetime.module
Issue summary: View changes

Updating title/IS to reflect where this patch went.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

This looks good to go!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: 2826404-10.patch, failed testing. View results

jofitz’s picture

Status: Needs work » Needs review
FileSize
50.72 KB

Patch in #10 no longer applies. Re-rolled.

larowlan’s picture

  1. +++ b/core/modules/datetime/datetime.module
    @@ -9,16 +9,28 @@
    + * @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use
    ...
    + * @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use
    ...
    + * @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use
    

    These need to reference the change record

  2. +++ b/core/modules/datetime/src/DateTimeComputed.php
    @@ -46,9 +47,9 @@ public function getValue($langcode = NULL) {
    +    $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
    

    Any reason why we're mixing the concrete Item and the ItemInterface? Should we be consistent? Moving the constant up from the Item to the Interface won't break BC

  3. +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php
    @@ -109,10 +109,10 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
    +      $values['value'] = gmdate(DateTimeItemInterface::DATE_STORAGE_FORMAT, $timestamp);
    ...
    +      $values['value'] = gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $timestamp);
    

    we can use self:: here

mpdonadio’s picture

Assigned: Unassigned » mpdonadio
Status: Needs review » Needs work

#15-2,

-    $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
+    $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : 

The constants on the Item already exist, and are related to the field type. The current patch adds DateTimeItemInterface, and replicates the storage constants in the global namespace. So, this is question about where to stop with scope for this issue. We can also move the existing constants on the Item, but then we would really should add a DateRangeItemInterface, too, and move that constant.

I'll post the other two comments later tonight.

mpdonadio’s picture

Assigned: mpdonadio » Unassigned
Status: Needs work » Needs review
FileSize
50.83 KB
2 KB

Here is #15. Per IRC w/ @larowlan, we will do the existing constants as a followup, #2915046: Move DateTimeItem and DateRangeItem constants onto interfaces.

mpdonadio’s picture

FileSize
658.3 KB
1.12 KB

Found a few more usages in comments.

jhedstrom’s picture

Status: Needs review » Needs work

I think #18 includes a bunch of un-intentional changes (perhaps diff happened after a coding standard issue was committed). I think this is good to go once fixed to include #17 + #18's interdiff.

mpdonadio’s picture

Status: Needs work » Needs review
FileSize
51.96 KB
1.12 KB

No idea what happened there (I have a shell script to make patches and interdiffs). This looks better.

Interdiff is against #17.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

Re-confirmed that all the usages of the old globals have been updated to the new class constants. This is great cleanup.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 20: 2826404-19.patch, failed testing. View results

mpdonadio’s picture

Status: Needs work » Reviewed & tested by the community

Odd bot problem that looks unrelated to this, https://www.drupal.org/pift-ci-job/794085

  • xjm committed 97346f0 on 8.5.x
    Issue #2826404 by mpdonadio, Jo Fitzgerald, jhedstrom, larowlan: Create...
xjm’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record updates

This is a great improvement.

  1. I thought we already deprecated all the global constants, but https://www.drupal.org/node/2831620 says "most". Should we add the newly deprecated constants to that CR as well? (We could add an "As of Drupal 8.5.x" section or something, to distinguish it from the older deprecations.)
  2. Normally we would have an @trigger_error() in the code path for the deprecated code. I couldn't think of any way to do this. We don't have a section for constants on https://www.drupal.org/core/deprecation. Our Symfony friends http://symfony.com/doc/current/contributing/code/conventions.html don't have an example for this either. So nothing actionable there.
  3. My initial thought was that datetime.module should use the interface to define the deprecated constants, but I see #4 already explains why that isn't possible.

I inspected the changed usages with git diff --staged --color-words="[^[:space:][:punct:]]+|[:punct:]+" and all look correct.

I also confirmed there are no remaining usages of the constants:

[ibnsina:maintainer | Tue 06:28:17] $ grep -r "DATETIME_STORAGE_TIMEZONE" *
core/modules/datetime/datetime.module:const DATETIME_STORAGE_TIMEZONE = 'UTC';
[ibnsina:maintainer | Tue 06:31:10] $ grep -r "DATETIME_DATETIME_STORAGE_FORMAT" *
core/modules/datetime/datetime.module:const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s';
[ibnsina:maintainer | Tue 06:31:23] $ grep -r "DATETIME_DATE_STORAGE_FORMAT " *
core/modules/datetime/datetime.module:const DATETIME_DATE_STORAGE_FORMAT = 'Y-m-d';

Committed and pushed to 8.5.x. Thanks! I published the new change record; let's also update https://www.drupal.org/node/2831620. I added a reference to this issue but did not update it with the additional rows. This can be marked back to fixed once the other CR is updated.

jhedstrom’s picture

Status: Needs work » Fixed

I've added those three constants to the CR noted in #25.

xjm’s picture

Thanks @jhedstrom.

Status: Fixed » Closed (fixed)

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