1

I'm setting a basic date field (and several other text fields) programmatically when creating a paragraph. The database is showing that it is getting my date, which is being stored inY-m-d\TH:i:s format. All the other data is being saved as well. No errors.

However, when I edit the node my paragraph is attached to, all the data is there except for the date field. It's not set despite the data being in the database.

The date field is set to show MDY in dropdowns like so:

enter image description here

I've tried this: $date = new DateTime('2015-06-01 23:45:52', new \DateTimeZone('UTC')); This does get the date saved into the database, however, the dropdowns are not set.

So then I tried converting it to MDY: $date->format('M-D-Y') but no such luck. This doesn't work at all.

I have also seen that you have to reference the actual stored format using $date->format(DATETIME_DATETIME_STORAGE_FORMAT) but there doesn't seem to be other parameters for that to get it into the format that I need.

Is there some way I need to individually set the actual dropdown options in code?

Full paragraph code below. Almost all of these examples save data in the DB correctly, the date dropdowns do not reference it though.

use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\file\Entity\File;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldFormatter\DateTimeCustomFormatter;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\datetime_range\DateTimeRangeTrait;    

  $date = new DateTime('2015-06-01 23:45:52', new \DateTimeZone('UTC'));
  //$date = $date->format('Y-m-d\TH:i:s');
  // if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
  //   $currentDate->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
  //   $db_friendly_value = $currentDate->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
  // }

  //$date->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
  //$date = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
  // $date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));

      $program_1 = Paragraph::create([
        'type' => 'programs',
        'field_program_name' => 'Program Name',
        'field_program_description' => 'Description',
        'field_program_year' => [
          'value' => $date,
        ],
      ]);
      $program_1->save();

Am I missing something somewhere? I've been hunting for the correct way to do this for a few days and have tried a number of different ways. I could probably get around this by just changing the field to be a text (date/time) date field instead of dropdowns.

I tried the suggestion here, but this doesn't seem to work either: Adding content to Paragraph Date Field Drupal 8

Any ideas here?

2
  • There is only one storage format const DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s'. You can't store it differently to get the format you need, this is what the form widget does when it gets the database value and converts it to the input format.
    – 4k4
    Apr 28 '20 at 7:17
  • I'm not trying to store it differently. It's storing perfectly fine. When I create the node programmatically with the paragraph attached and verify that the date is saved in the DB, the paragraph field is not set. So according to you, the widget just doesn't work. Is that correct? The current widget is set to "Date and Time" in a dropdown form just like I've posted the screen shot of. As I mentioned, the date is being saved in Y-m-d\TH:i:s but the widget is not loading it. Apr 28 '20 at 13:16
1

After trying just about every solution for the date I could find, I finally figured this out.

It seems that it doesn't even matter what the DB is wanting to save the date as. Looking in the DB, the date is being stored as Y-m-d without the trailing H:i:s. Most examples are saying that you need the full date in Y-m-d\TH:i:s format.

This particular dropdown widget for the date field wants only Y-m-d and I only found that out after trying to pass it all kinds of different date strings and it gave me an error message back about the format without the trailing H:i:s.

So all of that garbage that I tried to pass it earlier and formatting was way off. All I needed to do was this, and the paragraph saved properly and the form fields are reflecting the proper date now.

'field_program_year' => date('Y-m-d', strtotime('2011-11-12')),
'field_eligibility_range' => [
  'value'=> date('Y-m-d', strtotime('2011-11-12')),
  'end_value' => date('Y-m-d', strtotime('2011-11-12'))
],

enter image description here

2
  • 1
    Great find, might be very helpful for others. This is by the way not the particular widget but a field storage setting "Date only" and the correct storage format is then DateTimeItemInterface::DATE_STORAGE_FORMAT
    – 4k4
    Apr 29 '20 at 7:25
  • Yep, you're correct. The field storage for this is set to Date Only right now. Widget is set to Select List dropdowns, which was what I was having an issue setting. The date storage in the DB was never really an issue. Thank you for your input on this! Much appreciated! Apr 29 '20 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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