Date fields can be programmatically added like this:

$entity->get($field_name)->offsetGet(0)->setValue(array($iso_date));
// or
$entity->get($field_name)->offsetGet(0)->setValue(array('value' =>$iso_date));

But there is an exception when they are added like this:

$entity->get($field_name)->offsetGet(0)->get('value')->setValue($iso_date);

This works for text fields, so I would expect it to work for all fields.

Comments

linclark’s picture

Status: Active » Needs review
StatusFileSize
new4.47 KB

This patch introduces a DateTimeItemTest. It includes a test for what I think is a bug.

$entity->get($field_name)->offsetGet(0)->setValue(array('value' =>$iso_date));

This works for text fields but does not work for date. DateTimeItemTest::testSetValueProperty() tests for this and results in an Exception currently.

linclark’s picture

Title: Test the DateItem class » "value" property can not be set directly on DateTimeItems
Category: task » bug
Priority: Normal » Major

Changing the title to reflect the problem.

linclark’s picture

Issue summary: View changes

Updated issue summary.

Status: Needs review » Needs work

The last submitted patch, 2019587-01-datetimeitem-test.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
Issue tags: +Entity Field API
StatusFileSize
new4.95 KB

I knew I've seen this before.

This was initially the case consistently, the date field tests were failing horribly when I started converting them to use an NG entity. And then suddenly, it worked fine and I didn't know why.

The reason is the lazy loading of values. If you pass in the value on the DateItem, then the Date typed data object is never created, the value is simply stored in DateItem and then returned exactly as it was put in. However, if you do a setValue() on the date, then it ensures that it's a DrupalDateTime object and also returns that on ->value. Thats why currently the date fields are working.

I am not sure what to do. As I've said before, I think we need an method that returns a raw/storage/scalar representation of the value that can be used to store it somewhere, as you don't want to persist the date object.

As demonstration, the attached patch removes the DrupalDateTime check in Date::setValue(), this makes this test pass but then TypedDataTest explodes as it expects a date time object there.

Maybe the ongoing refactoring to use interfaces will help here as well, as we could do this: Date implements StringInterface, DateInterface. Then getValue() (or getString() but that's IMHO tricky) will return the date string and getDate() will return a DrupalDateTime object.

Status: Needs review » Needs work

The last submitted patch, 2019587-04-datetimeitem-test.patch, failed testing.

berdir’s picture

#2002102: Move TypedData primitive types to interfaces is the issue that I mentioned and the latest iteration of the Date class there should fix this.

I'd suggest to keep this open and push the interface issue, then we can just keep open as a reminder and to add test-coverage. Or we could move the test coverage over there, but the might might already get quite big...

fago’s picture

berdir’s picture

Status: Needs work » Needs review
Issue tags: -Entity Field API

#4: 2019587-04-datetimeitem-test.patch queued for re-testing.

berdir’s picture

#1: 2019587-01-datetimeitem-test.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Entity Field API

The last submitted patch, 2019587-04-datetimeitem-test.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review

Setting to needs review, test-patch is still running, re-tested the wrong one.

berdir’s picture

Status: Needs review » Needs work

Fatal error, field_create_field() no longer exists.

berdir’s picture

Title: "value" property can not be set directly on DateTimeItems » Add test coverage for DateTimeItem
Category: bug » task
Priority: Major » Normal
Status: Needs work » Needs review
StatusFileSize
new1.07 KB
new4.54 KB

Updated the field creation, test is green now :)

Which means this is just about adding additional test coverage.

linclark’s picture

Status: Needs review » Needs work
+++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeItemTest.phpundefined
@@ -0,0 +1,127 @@
+    // WHY DOES THIS FAIL????
+    // @todo Make this not fail.

Sorry, this is my bad... I'll try to reroll if I get a chance. I'm giving a few talks this afternoon at NYC Camp, so this might not be until later.

linclark’s picture

Status: Needs work » Needs review
StatusFileSize
new755 bytes
new4.48 KB

Ok, removed the comment.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Yay more test coverage.

I only changed the field_create_field() call to entity_create(), so I think it's ok if I RTBC this.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed b11b7fa and pushed to 8.x. Thanks!

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

Anonymous’s picture

Issue summary: View changes

Updated code example in issue summary.