Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Views field custom date field output for year only #439

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/Plugin/views/field/CustomEntityField.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ protected function getItemValue($value, FieldDefinitionInterface $definition) {
switch ($definition->getType()) {
case 'datetime':
if (!empty($value)) {
$datetime_format = $definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
return (new \DateTime($value, new \DateTimeZone(date_default_timezone_get())))->setTimezone(new \DateTimeZone('UTC'))->format($datetime_format);
return $this->convertToUtc($definition, $value);
}
break;

Expand Down Expand Up @@ -335,4 +334,40 @@ protected function getDelta($id) {
return 0;
}

/**
* Check if date field should be converted to UTC or not.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $definition
* The field definition.
* @param string $date_value
* The date value.
*
* @return string
* The converted value.
*/
public function convertToUtc(FieldDefinitionInterface $definition, $date_value) {
$datetime_format = $definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
$default_timezone = date_default_timezone_get();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the difference from PR #435 for the 3.x branch

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If using the global configuration then if the per-user timezone setting is allowed, and if the user chooses a different timezone, then could have a discrepancy. Drupal manages this and gives the "right" timezone returned by date_default_timezone_get();


$utc = TRUE;
// If the field is custom and meant to store only year value,
// Avoid converting to any timezone and display it as stored in database.
if (strpos($definition->getName(), "custom_") === 0) {
[, $custom_field_id] = explode('_', $definition->getName());
$params = [
'sequential' => 1,
'id' => $custom_field_id,
];
$date_field = $this->civicrmApi->get('CustomField', $params);
if (!empty($date_field[0]['date_format']) && $date_field[0]['date_format'] === 'yy') {
$utc = FALSE;
}
}

if ($utc) {
return (new \DateTime($date_value, new \DateTimeZone($default_timezone)))->setTimezone(new \DateTimeZone('UTC'))->format($datetime_format);
}
return (new \DateTime($date_value, new \DateTimeZone($default_timezone)))->format($datetime_format);
}

}
Loading