0

I'm programmatically creating paragraphs using the following code, but I'm unable to get the date value to store correctly within the date field. It seems to store within the database, yet on the edit page/form for the node, it remains as a blank field.

Any suggestions or insight would be appreciated.

//SET THE DATE
$date = new DrupalDateTime('2017-10-24 13:30:00');

//CREATE THE NEW PARAGRAPH
$paragraph = Paragraph::create([
    'type' => 'event_venue',
    'field_tickets' => [
        ["value"  =>  'Adult Update 1|£30'],
        ["value"  =>  'Adult Update 2|£130']
        ],
    'field_date' => array($date->format('Y-m-d H:i:s')),

]);

//SAVE THE PARAGRAPH SO WE CAN ADD THE ENTITY ID TO THE NODE
$paragraph->save();

//LOAD THE NODE
$node = entity_load('node', $nid);

//ADD PARAGRAPH TO THE NODE
$node->field_dates = array(
    array(
      'target_id' => $venue_information->id(),
      'target_revision_id' => $venue_information->getRevisionId(),
    )
);

//SAVE THE NODE
$nid = $node->save();
1

Use the global constant DATETIME_DATETIME_STORAGE_FORMAT to format the date:

// If the time is in a different timezone then adjust it to UTC before you save it
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));

$paragraph = Paragraph::create([
  ...
  'field_date' => [
    'value' => $date->format(DATETIME_DATETIME_STORAGE_FORMAT),
  ],
]);

Edit

For Drupal >= 8.5: Global constants in datetime.module are deprecated and DateTimeItemInterface has been introduced.

0

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.