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:
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?
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.Y-m-d\TH:i:s
but the widget is not loading it.