DateTimeFieldMapping.php in Webform Content Creator 3.x
File
src/Plugin/WebformContentCreator/FieldMapping/DateTimeFieldMapping.php
View source
<?php
namespace Drupal\webform_content_creator\Plugin\WebformContentCreator\FieldMapping;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_content_creator\Plugin\FieldMappingBase;
class DateTimeFieldMapping extends FieldMappingBase {
public function getSupportedWebformFields($webform_id) {
$supported_types = [
"changed",
"created",
"date",
"datetime",
"datelist",
"timestamp",
"webform_time",
];
return $this
->filterWebformFields($webform_id, $supported_types);
}
public function mapEntityField(ContentEntityInterface &$content, array $webform_element, array $data = [], FieldDefinitionInterface $field_definition) {
$field_id = $field_definition
->getName();
$field_value = $data[$field_id];
if ($field_definition
->getType() == "datetime") {
$field_data = $this
->convertTimestamp($field_value, $field_definition);
$content
->set($field_id, $field_data);
}
}
protected function convertTimestamp($value, FieldDefinitionInterface $field_definition) {
$date_time = new DrupalDateTime($value, 'UTC');
$date_type = $field_definition
->getSettings()['datetime_type'];
if ($date_type === 'datetime') {
$result = \Drupal::service('date.formatter')
->format($date_time
->getTimestamp(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, 'UTC');
}
else {
$result = \Drupal::service('date.formatter')
->format($date_time
->getTimestamp(), 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, 'UTC');
}
return $result;
}
}