Score:0

Cannot get entity form to display date field value

cl flag

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.

Score:0
cn flag

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.

cl flag
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.
cn flag
@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?
cl flag
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.
cn flag
@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.
cl flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.