1

I am trying to copy the value of a Date/Time Webform element to a Date/Time field set on the article node.

Below is the source code of the Date/Time Webform element:

expiry_date:
  '#type': datetime
  '#title': 'Expiry Date'
  '#title_display': before
  '#required': true
  '#required_error': 'You must add an expiry date'
  '#date_date_format': ''
  '#date_year_range': '2019:2100'
  '#date_time_format': ''

Below is my custom handler:

// Get an array of the values from the submission.
    $values = $webform_submission->getData();

    // Get 'Expiry Date' Value.
    $ExpiryDate = $values['expiry_date'];
    date_default_timezone_set('UTC');   // Sett the time to 'UTC' time zone.

    // Format the "Expiry Date" to return "Y-m-d\TH:i:s".
    //$FormattedExpiry = date('Y-m-d\TH:i:s', $ExpiryDate);


// Create a new "Article" node.
    $article = \Drupal\node\Entity\Node::create([
        'type' => 'article',
    // Set the node field values.        
        'title' => $Title,
        'langcode' => 'en',
        'uid' => $CurrentUid,
        'status' => '1',
        'created' => $current_time,
        'field_expiry_date' => $ExpiryDate, // $FormattedExpiry,
        ]);

// Save the created node.
    $article->save()

But I am facing the below error:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'field_expiry_date_value' at row 1: INSERT INTO {node__field_expiry_date} (entity_id, revision_id, bundle, delta, langcode, field_expiry_date_value) VALUES (
:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5);
Array ( 
[:db_insert_placeholder_0] => 84
[:db_insert_placeholder_1] => 88
[:db_insert_placeholder_2] => article
[:db_insert_placeholder_3] => 0
[:db_insert_placeholder_4] => en
[:db_insert_placeholder_5] => 2021-05-25 11:26:02 +03:00 
)
in Drupal\Core\Entity\Sql\SqlContentEntityStorage->saveToDedicatedTables() (line 1414 of /home/username/public_html/example.com/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
2

The expiry_date from webform return something like 2021-05-25 11:26:02 +03:00 so you should to format it before you passe it to create like the following:

// Get 'Expiry Date' Value.
$ExpiryDate = $values['expiry_date'];
// Format the "Expiry Date" to return "Y-m-d\TH:i:s".
// Here use strtotime to format date correctly 
$FormattedExpiry = date('Y-m-d\TH:i:s', strtotime($ExpiryDate));

Then passe $FormattedExpiry instead of $ExpiryDate:

$article = \Drupal\node\Entity\Node::create([
    'type' => 'article',
   // Set the node field values.        
    'title' => $Title,
    'langcode' => 'en',
    'uid' => $CurrentUid,
    'status' => '1',
    'created' => $current_time,
    'field_expiry_date' => $FormattedExpiry,
    ]);

Update: your date already has +3h, if you want to not add 3 hours to your time there is two options i guess:

  1. Change you webform field settings to work with UTC to get the value of $values['expiry_date'] something like 2021-05-25 11:26:02 without +03:00
  2. Remove the +03:00 before date conversion from $values['expiry_date'] by change the line $ExpiryDate = $values['expiry_date']; with something like:

    $ExpiryDate = explode('+', $values['expiry_date'])[0];

| improve this answer | |
  • Thank you your code is working ! However, I am getting the time added by 3 hours (UTC Time). How to change the time zone or maybe use the timezone of the site ? – user98752 Apr 25 at 7:20
  • You need to store the time as UTC and store the offset for your time zone separately. See my answer here: drupal.stackexchange.com/a/273372/80164 – hotwebmatter Apr 25 at 15:06
  • Actually your expire date in the webform stored with +3h, look in the error message the date is 2021-05-25 11:26:02 +03:00 so i guess, check how to make webform use the time zone of your site or UTC – berramou Apr 25 at 15:46
  • check my update, the suggested method by @ hotwebmatter will not work, because date it will calculated before based on +03:00 – berramou Apr 25 at 16:07
  • 1
    Finally it is working when I used date_default_timezone_set('UTC'); // Sett the time to 'UTC' time zone., check the updated question – user98752 Apr 25 at 22:12

Your Answer

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