I'm wondering how I can grab the date from a datepicker in a form (format is d/m/Y) and store it into my content entity. The form is produced from said content entity, so I don't see why there should be any issues between formats - but it doesn't copy directly. I know the date fields are stored as varchar(20) in the database, so it must be a string at some stage!

I'm grabbing the value from my form as below:

$dob = $form_state->get('field_dob');
$given = new DrupalDateTime($dob, \Drupal::currentUser()->getTimezone());
$given->setTimezone(new \DateTimeZone("UTC"));

This gives me a string formatted as per the storage format, yet still won't copy across to the corresponding content entity.

Also, this is with Drupal 8.1.x. :)

Any insight would be much appreciated, thanks!

  • 1
    The crucial part of the code seems to be missing. how do you assign $dob to your entity? – Berdir Sep 7 '16 at 18:09
  • @Berdir $private_profile->set('field_dob', $dob); – Shannon Rothe Sep 8 '16 at 3:03
  • If that's a normal content entity form, isn't field_dob an array with nested elements? Why don't you get those values from the already built content entity and just assign $entity->field_dob->value to the other entity? – Berdir Sep 8 '16 at 6:45

Your Answer

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

Browse other questions tagged or ask your own question.