Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
calendar_view-3427245
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
calendar_view-3427245
Commits
6cd7527e
Commit
6cd7527e
authored
1 year ago
by
Matthieu Scarset
Browse files
Options
Downloads
Patches
Plain Diff
feat: Cleaner timezone code
parent
62b5a049
No related branches found
Branches containing commit
Tags
2.1.4
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/views/style/CalendarViewBase.php
+51
-42
51 additions, 42 deletions
src/Plugin/views/style/CalendarViewBase.php
with
51 additions
and
42 deletions
src/Plugin/views/style/CalendarViewBase.php
+
51
−
42
View file @
6cd7527e
...
...
@@ -176,6 +176,48 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
return
$date_fields
;
}
/**
* Determine timezone relative to a given date field or to the current user.
*
* @param \Drupal\views\Plugin\views\field\EntityField $field
* (optional) A given date field.
*
* @return string The timezone, as a string.
*/
public
function
getTimezone
(
EntityField
$field
=
NULL
)
{
$timezone
=
$this
->
dateConfig
->
get
(
'timezone'
)[
'default'
];
// Get user's timezone, if enabled.
if
(
$this
->
dateConfig
->
get
(
'timezone.user.configurable'
))
{
$timezone
=
$this
->
currentUser
->
getTimeZone
()
?:
$timezone
;
}
// Get field overridden timezone.
if
(
$field
&&
isset
(
$field
->
options
[
'settings'
][
'timezone_override'
]))
{
$timezone
=
$field
->
options
[
'settings'
][
'timezone_override'
]
?:
$timezone
;
}
return
$timezone
;
}
/**
* Calculate time offset between two timezones.
*
* @param string $time
* A date/time string compatible with \DateTime. It is used as the
* reference for computing the offset, which can vary based on the time
* zone rules.
* @param string $timezone
* The time zone that $time is in.
*
* @return int
* The computed offset in seconds.
*
* @see \Drupal\datetime\Plugin\views\filter\Date::getOffset()
*/
public
function
getTimezoneOffset
(
string
$time
,
string
$timezone
)
{
$tz
=
new
\DateTimeZone
(
$timezone
);
return
$tz
->
getOffset
(
new
\DateTime
(
$time
,
$tz
));
}
/**
* {@inheritDoc}
*/
...
...
@@ -420,41 +462,6 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
return
parent
::
render
();
}
/**
* Determine what timezone to use before rendering.
*
* @param \Drupal\views\Plugin\views\field\EntityField $field
* (optional) A given date field.
* @param \Drupal\Core\Session\AccountInterface $account
* (optional) A given user session.
*
* @return array
* The timezone and the offset values.
*/
public
function
getTimezoneData
(
EntityField
$field
=
NULL
,
AccountInterface
$account
=
NULL
)
{
$account
=
$account
??
$this
->
currentUser
;
$timezone
=
'UTC'
;
if
(
!
empty
(
$field
->
options
[
'settings'
][
'timezone_override'
]))
{
$timezone
=
$field
->
options
[
'settings'
][
'timezone_override'
];
}
elseif
((
$user_timezone
=
$account
->
getTimezone
())
&&
!
empty
(
$user_timezone
))
{
$timezone
=
$user_timezone
;
}
elseif
((
$system_timezone
=
$this
->
dateConfig
->
get
(
'timezone'
)[
'default'
])
&&
!
empty
(
$system_timezone
))
{
$timezone
=
$system_timezone
;
}
$tz
=
new
\DateTimeZone
(
$timezone
);
$time_now
=
new
\DateTime
(
'now'
,
$tz
);
$offset
=
$tz
->
getOffset
(
$time_now
);
return
[
'timezone'
=>
$timezone
,
'offset'
=>
$offset
,
];
}
/**
* Get the value out of a view Result for a given date field.
*
...
...
@@ -489,18 +496,22 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
$values
[
'end_value'
]
=
(
$this
->
ensureTimestampValue
(
$values
[
'end_value'
]
??
$values
[
'value'
]));
// Get offset to fix start/end datetime values.
$timezone
=
$this
->
getTimezoneData
(
$field
);
$values
[
'value'
]
+=
$timezone
[
'offset'
]
??
0
;
$values
[
'end_value'
]
+=
$timezone
[
'offset'
]
??
0
;
$timezone
=
$this
->
getTimezone
(
$field
);
$offset
=
$this
->
getTimezoneOffset
(
'now'
,
$timezone
);
$values
[
'value'
]
+=
$offset
;
$values
[
'end_value'
]
+=
$offset
;
// Get first item value to reorder multiday events in cells.
$all_values
=
$field
->
getValue
(
$row
);
$all_values
=
\is_array
(
$all_values
)
?
$all_values
:
[
$all_values
];
$first_value
=
reset
(
$all_values
);
// Transform ISO8601 to timestamp.
if
(
!
ctype_digit
(
$first_value
))
{
$first_instance_date
=
new
DateTimePlus
(
$first_value
);
$first_value
=
$first_instance_date
->
getTimestamp
();
}
$values
[
'first_instance'
]
=
(
int
)
$first_value
;
// Expose the date field if other modules need it in preprocess.
...
...
@@ -518,7 +529,6 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
$start
=
$values
[
'value'
];
$end
=
$values
[
'end_value'
]
??
$start
;
$title_string
=
$start
&&
(
$start
!==
$end
)
?
'@title from @start to @end (@timezone)'
:
'@field: @date (@timezone)'
;
$tz
=
$timezone
[
'timezone'
]
??
'UTC'
;
$values
[
'title'
]
=
$this
->
t
(
$title_string
,
[
'@title'
=>
$entity
->
label
(),
...
...
@@ -526,7 +536,7 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
'@date'
=>
$this
->
dateFormatter
->
format
(
$start
,
'long'
),
'@start'
=>
$this
->
dateFormatter
->
format
(
$start
,
'short'
),
'@end'
=>
$this
->
dateFormatter
->
format
(
$end
,
'short'
),
'@timezone'
=>
$t
z
,
'@timezone'
=>
$t
imezone
,
]);
return
$values
;
...
...
@@ -551,9 +561,8 @@ abstract class CalendarViewBase extends DefaultStyle implements CalendarViewInte
return
;
}
$timezone
=
$this
->
getTimezoneData
();
/** @var \Drupal\Core\Datetime\DrupalDateTime $now */
$now
=
new
DrupalDateTime
(
''
,
$t
imezone
[
'timezone'
]
??
NULL
);
$now
=
new
DrupalDateTime
(
''
,
$t
his
->
getTimezone
()
);
$start_day
=
clone
$now
;
$start_day
->
setTimestamp
(
$start
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment