DateTimeExport.php in Entity Export CSV 8
File
src/Plugin/FieldTypeExport/DateTimeExport.php
View source
<?php
namespace Drupal\entity_export_csv\Plugin\FieldTypeExport;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
use Drupal\entity_export_csv\Plugin\FieldTypeExportBase;
use Drupal\Core\Field\FieldItemInterface;
class DateTimeExport extends FieldTypeExportBase {
protected $dateFormats = [];
public function getSummary() {
return [
'message' => [
'#markup' => $this
->t('Datetime field type exporter.'),
],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FieldDefinitionInterface $field_definition) {
$configuration = $this
->getConfiguration();
$build = parent::buildConfigurationForm($form, $form_state, $field_definition);
$build['custom_date_format'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom date format'),
'#description' => $this
->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
'#default_value' => !empty($configuration['custom_date_format']) ? $configuration['custom_date_format'] : '',
];
$build['custom_date_format']['#states']['visible'][] = [
':input[name="fields[' . $field_definition
->getName() . '][form][options][format]"]' => [
'value' => 'custom',
],
];
return $build;
}
public function massageExportPropertyValue(FieldItemInterface $field_item, $property_name, FieldDefinitionInterface $field_definition, $options = []) {
if ($field_item
->isEmpty()) {
return NULL;
}
$configuration = $this
->getConfiguration();
if (empty($configuration['format'])) {
return $field_item
->get($property_name)
->getValue();
}
$langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$timezone = NULL;
$format = NULL;
$settings = [
'langcode' => $langcode,
];
$date_format_id = $configuration['format'];
$custom_date_format = !empty($configuration['custom_date_format']) ? $configuration['custom_date_format'] : '';
if ($date_format_id === 'custom' && $custom_date_format === 'r') {
$langcode = 'en';
$settings['langcode'] = $langcode;
}
if (!empty($custom_date_format) && $date_format_id === 'custom') {
$format = $custom_date_format;
}
else {
if ($date_format = $this
->getDateFormat($date_format_id, $langcode)) {
$format = $date_format
->getPattern();
}
}
$date = $field_item->date;
if ($date instanceof DrupalDateTime && $format) {
return $date
->format($format, $settings);
}
return $field_item
->get($property_name)
->getValue();
}
protected function getFormatExportOptions(FieldDefinitionInterface $field_definition) {
$options = parent::getFormatExportOptions($field_definition);
$date_formats = [];
foreach ($this->entityTypeManager
->getStorage('date_format')
->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $this
->t('@name format: @date', [
'@name' => $value
->label(),
'@date' => $this->dateFormatter
->format(REQUEST_TIME, $machine_name),
]);
}
$date_formats['custom'] = $this
->t('Custom');
return $options + $date_formats;
}
protected function getDateFormat($format, $langcode) {
if (!isset($this->dateFormats[$format][$langcode])) {
$original_language = $this->languageManager
->getConfigOverrideLanguage();
$this->languageManager
->setConfigOverrideLanguage(new Language([
'id' => $langcode,
]));
$this->dateFormats[$format][$langcode] = $this->entityTypeManager
->getStorage('date_format')
->load($format);
$this->languageManager
->setConfigOverrideLanguage($original_language);
}
return $this->dateFormats[$format][$langcode];
}
}