Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When generating content using devel_generate
I noticed a field of type timestamp didn't produced default values.
It seems Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem
doesn't provides a method generateSampleValue
Proposed resolution
Implement the method on the given class.
Remaining tasks
None
User interface changes
None
API changes
New method TimestampItem::generateSampleValue().
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#21 | interdiff-2802541-18-21.txt | 1.74 KB | dagmar |
#21 | 2802541-21.patch | 4.08 KB | dagmar |
#21 | 2802541-21-test-only.patch | 3.02 KB | dagmar |
#18 | interdiff-2802541-16-18.txt | 1.33 KB | dagmar |
#18 | 2802541-18.patch | 3.95 KB | dagmar |
Comments
Comment #2
dagmarHere is the patch.
Comment #3
dagmarComment #4
dagmarComment #6
dagmarComment #8
dagmarComment #9
mpdonadioThanks for reporting and fixing this. Quick, first look here...
I think this is an out of scope change, and I don't think this really belongs in TimestampFormatterTest. Maybe create TimestampItemTest and put it there? Can we also post a test-only patch? The method exists on the class, it is just a no-op because the base class is an empty function. So, this should be eligible for 8.2.x in a point release as a bugfix.
Little hesitant to use `REQUEST_TIME` directly here, but injecting the request stack to get it out of the request is seriously overkill for this situation. Tempted to leave this as-is, and then handle it as part of the followups to #2717207: Add a time service to provide consistent interaction with time()/microtime() and superglobals.
Will take a closer look at this once you think you have it settled out.
Comment #10
mpdonadioOK, looked at this in place.
How about "Pick a random timestamp in the past year."
This is an out-of-scope change. Let's create TimestampItemTest as a FieldKernelTestBase instead.
This test doesn't fail when I revert the change to TimestampItem. I suspect it is because of PHP weirdness, and an empty value is getting cast to a 0 somewhere, which is a valid timestamp.
Comment #12
dagmarSorry for the delay :P
It seems we don't have any tests for timestamps fields. This should cover the basics.
Comment #14
dagmarComment #15
mpdonadioOK, this is pretty good. The fail in #12 for 8.4 looks unrelated, but this still needs a little work. The main problem is that TimestampItem has no real relation to DatetimeItem, or the datetime.module. We need to make sure we don't couple them in the test.
How about "Pick a random timestamp in the past year."
We need to place this outside the datetime module. The `Drupal\Tests\field\Kernel\Timestamp` namespace is probably better.
@group field would be better.
Should be TimestampItem test, to match the field class.
OK, this is the crux of the last few comments :) TimestampItem is provided by Core, not datetime.module, so we need to explicitly make sure this isn't installed.
Missing the @{inheritdoc}
Not sure why this is needed; that setting doesn't exist on timestamps.
Not quite sure what this is testing?
Comment #16
dagmarThanks @mpdonadio. He is the new patch with your suggestions.
Comment #17
mpdonadioSorry for the delay on final review of this. Just a few small things.
Nit; should be alphabetical.
To be consistent with other tests, let's just say "Test sample item generation."
We can get rid of this. None of the other item tests seem to do this.
Verified TimestampItemTest is being picked up. If we fix these, we are RTBC. We may want some followups to enhance the test coverage, but that is lower priority.
Comment #18
dagmarThanks @mpdonadio
Comment #19
mpdonadioSorry for the delay; missed it in my last NR sweep. This looks good to me.
Comment #20
lauriiiThe bug fix itself looks good for me. I also updated the issue credit.
KernelTestBase::assertEqual has been deprecated. Could you also post a failing test patch because some of the assertions have been removed since the last failing test?
Thank you for adding the additional test coverage for the
TimestampItem
.Comment #21
dagmarThanks @lauriii. Added back the assertion to check the default value, because a method in the parent class that actually returns null.
Comment #23
mpdonadioLooks good to me.
Comment #24
alexpott@lauriii asked me about the abstract class implementing an empty generateSampleValue - I agreed this was odd and that we should remove this in 9.0.0 to ensure that all field types implement this properly. Opened #2872790: Deprecate \Drupal\Core\Field\FieldItemBase::generateSampleValue
Comment #25
lauriiiThanks @alexpott, that makes sense.
Committed 02cd2f4 and pushed to 8.4.x. Thanks!