protected function ContactStorageExportService::formatDateTime in Contact Storage Export 8
Formats a date-time field item with a given format.
Parameters
\Drupal\Core\Field\FieldItemInterface $item: Afield item to format.
string $format: A date format. Defaults to short format (m/d/Y - H:i).
Return value
string Formatted date-time value.
1 call to ContactStorageExportService::formatDateTime()
- ContactStorageExportService::getFormattedValue in src/
ContactStorageExportService.php - Formats values of the given field.
File
- src/
ContactStorageExportService.php, line 185
Class
- ContactStorageExportService
- Provides a set of methods to export contact messages in CSV format.
Namespace
Drupal\contact_storage_exportCode
protected function formatDateTime(FieldItemInterface $item, $format) {
$format = $format ?: 'short';
$settings = [];
if ($item instanceof CreatedItem) {
$settings['date_format'] = $format;
}
elseif ($item instanceof DateRangeItem) {
// Remove new lines between start and end dates.
$date_range_item_view = $item
->view([
'settings' => [
'format_type' => $format,
],
]);
$markup = $this
->renderAsString($date_range_item_view);
return str_replace(PHP_EOL, '', $markup);
}
elseif ($item instanceof DateTimeItem) {
$settings['format_type'] = $format;
}
$date_item_view = $item
->view([
'settings' => $settings,
]);
return $this
->renderAsString($date_item_view);
}