X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fdatetime%2Fsrc%2FPlugin%2FField%2FFieldFormatter%2FDateTimeFormatterBase.php;fp=web%2Fcore%2Fmodules%2Fdatetime%2Fsrc%2FPlugin%2FField%2FFieldFormatter%2FDateTimeFormatterBase.php;h=98b1ea3857f9767351dae171a77e5c69eec18875;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=f4e5ff53539271c1200335f57013664702391042;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php b/web/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php index f4e5ff535..98b1ea385 100644 --- a/web/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php +++ b/web/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php @@ -6,13 +6,13 @@ use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; use Symfony\Component\DependencyInjection\ContainerInterface; - /** * Base class for 'DateTime Field formatter' plugin implementations. */ @@ -97,7 +97,7 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF '#type' => 'select', '#title' => $this->t('Time zone override'), '#description' => $this->t('The time zone selected here will always be used'), - '#options' => system_time_zones(TRUE), + '#options' => system_time_zones(TRUE, TRUE), '#default_value' => $this->getSetting('timezone_override'), ]; @@ -117,6 +117,30 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF return $summary; } + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items, $langcode) { + $elements = []; + + foreach ($items as $delta => $item) { + if ($item->date) { + /** @var \Drupal\Core\Datetime\DrupalDateTime $date */ + $date = $item->date; + $elements[$delta] = $this->buildDateWithIsoAttribute($date); + + if (!empty($item->_attributes)) { + $elements[$delta]['#attributes'] += $item->_attributes; + // Unset field item attributes since they have been included in the + // formatter output and should not be rendered in the field template. + unset($item->_attributes); + } + } + } + + return $elements; + } + /** * Creates a formatted date value as a string. * @@ -167,4 +191,69 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF return $settings; } + /** + * 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 = [ + '#markup' => $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; + } + }