0

I have an entity with two date fields. In the code that saves the entity, is:

$date_requested = date('Y-m-d', time());

and in the list of field values that get added to the entity:

->set('field_date_requested', $date_requested)

In inspecting the db, the table contains:

bundle | deleted | entity_id | revision_id | langcode | delta|field_date_requested_value
+-------+---------+-----------+-------------+----------+------+--------------+
| my_bundle |       0 |         4 |           4 | en   |    0 | 2022-05-26       

But when I edit the entity, I see this:

screen shot of entity field display

If I insert the date using the same format as the placeholder, it is in the db with that format, but still does not display. If I enter the date in the entity form manually (which requires it to be m/d/Y), it displays when edited again, in that format, though I see no difference in the value subfield in the db.

1 Answer 1

0

When storing datetimes in the database, you should use DateTimeItemInterface::DATETIME_STORAGE_FORMAT, which is Y-m-d\\TH:i:s. If you store it in a different format, Drupal will fail to process it correctly.

5
  • Changed the format to that. Now the value in the db is 2022-05-26T00:00:00, but the entity form still displays an empty field with a placeholder.
    – JAyenGreen
    May 25, 2022 at 2:08
  • @JAyenGreen Other than setting the value, is the value being modified in any way by your code? What is the value being sent to the widget that is failing to display the value? May 25, 2022 at 4:31
  • It’s a simple string in yyyy-mm-dd format. It’s generated by date(fmt, time stamp). The Drupal date field is just a MySQL varchar(20), and in the db it is displayed properly. As the original date selection by the user is made using FullCalendar, not the Date widget, I’m beginning to think I would be better off with the field being plain text.
    – JAyenGreen
    May 25, 2022 at 10:09
  • @JAyenGreen Ok, I don't know anything about FullCalendar, but if you are using a Date widget other than the one that comes with Drupal core, that should be part of the question. May 25, 2022 at 13:30
  • I’m not. The calendar is used for selecting a date. I then take that value as a yyyy-mm-did string and save it to the entity with the other entity values.
    – JAyenGreen
    May 26, 2022 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.