Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0
This commit is contained in:
parent
2f563ab520
commit
f1c8716f57
1732 changed files with 52334 additions and 11780 deletions
78
core/modules/datetime_range/src/DateTimeRangeTrait.php
Normal file
78
core/modules/datetime_range/src/DateTimeRangeTrait.php
Normal file
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime_range;
|
||||
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
|
||||
/**
|
||||
* Provides friendly methods for datetime range.
|
||||
*/
|
||||
trait DateTimeRangeTrait {
|
||||
|
||||
/**
|
||||
* Creates a render array from a date object.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object.
|
||||
*
|
||||
* @return array
|
||||
* A render array.
|
||||
*/
|
||||
protected function buildDate(DrupalDateTime $date) {
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$build = [
|
||||
'#plain_text' => $this->formatDate($date),
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a render array from a date object with ISO date attribute.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object.
|
||||
*
|
||||
* @return array
|
||||
* A render array.
|
||||
*/
|
||||
protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
|
||||
// Create the ISO date in Universal Time.
|
||||
$iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
|
||||
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$build = [
|
||||
'#theme' => 'time',
|
||||
'#text' => $this->formatDate($date),
|
||||
'#html' => FALSE,
|
||||
'#attributes' => [
|
||||
'datetime' => $iso_date,
|
||||
],
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue