diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DateTimeDefaultFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DateTimeDefaultFormatter.php new file mode 100644 index 0000000..a2ea342 --- /dev/null +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DateTimeDefaultFormatter.php @@ -0,0 +1,188 @@ +dateService = $date_service; + $this->dateStorage = $date_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $container->get('date'), + $container->get('plugin.manager.entity')->getStorageController('date_format') + ); + } + + /** + * {@inheritdoc} + */ + public function viewElements(EntityInterface $entity, $langcode, FieldInterface $items) { + + $elements = array(); + + foreach ($items as $delta => $item) { + + $formatted_date = ''; + $iso_date = ''; + + if (!empty($item->date)) { + // The date was created and verified during field_load(), so it is safe + // to use without further inspection. + $date = $item->date; + + // Create the ISO date in Universal Time. + $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z'; + + // The formatted output will be in local time. + $date->setTimeZone(timezone_open(drupal_get_user_timezone())); + if ($this->getFieldSetting('datetime_type') == 'date') { + // A date without time will pick up the current time, use the default. + datetime_date_default_time($date); + } + $formatted_date = $this->dateFormat($date); + } + + // Display the date using theme datetime. + // @todo How should RDFa attributes be added to this? + $elements[$delta] = array( + '#theme' => 'datetime', + '#text' => $formatted_date, + '#html' => FALSE, + '#attributes' => array( + 'datetime' => $iso_date, + 'property' => array('dc:date'), + 'datatype' => 'xsd:dateTime', + ), + ); + } + + return $elements; + + } + + /** + * Creates a formatted date value as a string. + * + * @param object $date + * A date object. + * + * @return string + * A formatted date string using the chosen format. + */ + function dateFormat($date) { + $format_type = $this->getSetting('format_type'); + return $this->dateService->format($date->getTimestamp(), $format_type); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, array &$form_state) { + $time = new DrupalDateTime(); + $format_types = $this->dateStorage->loadMultiple(); + foreach ($format_types as $type => $type_info) { + $format = $this->dateService->format($time->format('U'), $type); + $options[$type] = $type_info->label() . ' (' . $format . ')'; + } + + $elements['format_type'] = array( + '#type' => 'select', + '#title' => t('Date format'), + '#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."), + '#options' => $options, + '#default_value' => $this->getSetting('format_type'), + ); + + return $elements; + } + + /** + * {@inheritdoc} + */ + public function settingsSummary() { + $summary = array(); + $date = new DrupalDateTime(); + $summary[] = t('Format: @display', array('@display' => $this->dateFormat($date, FALSE))); + return $summary; + } + +} diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DateTimePlainFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DateTimePlainFormatter.php new file mode 100644 index 0000000..2eefd13 --- /dev/null +++ b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DateTimePlainFormatter.php @@ -0,0 +1,59 @@ + $item) { + + $output = ''; + if (!empty($item->date)) { + // The date was created and verified during field_load(), so it is safe + // to use without further inspection. + $date = $item->date; + $date->setTimeZone(timezone_open(drupal_get_user_timezone())); + $format = DATETIME_DATETIME_STORAGE_FORMAT; + if ($this->getFieldSetting('datetime_type') == 'date') { + // A date without time will pick up the current time, use the default. + datetime_date_default_time($date); + $format = DATETIME_DATE_STORAGE_FORMAT; + } + $output = $date->format($format); + } + $elements[$delta] = array('#markup' => $output); + } + + return $elements; + } + +} diff --git a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php b/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php deleted file mode 100644 index a2ea342..0000000 --- a/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimeDefaultFormatter.php +++ /dev/null @@ -1,188 +0,0 @@ -dateService = $date_service; - $this->dateStorage = $date_storage; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { - return new static( - $plugin_id, - $plugin_definition, - $configuration['field_definition'], - $configuration['settings'], - $configuration['label'], - $configuration['view_mode'], - $container->get('date'), - $container->get('plugin.manager.entity')->getStorageController('date_format') - ); - } - - /** - * {@inheritdoc} - */ - public function viewElements(EntityInterface $entity, $langcode, FieldInterface $items) { - - $elements = array(); - - foreach ($items as $delta => $item) { - - $formatted_date = ''; - $iso_date = ''; - - if (!empty($item->date)) { - // The date was created and verified during field_load(), so it is safe - // to use without further inspection. - $date = $item->date; - - // Create the ISO date in Universal Time. - $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z'; - - // The formatted output will be in local time. - $date->setTimeZone(timezone_open(drupal_get_user_timezone())); - if ($this->getFieldSetting('datetime_type') == 'date') { - // A date without time will pick up the current time, use the default. - datetime_date_default_time($date); - } - $formatted_date = $this->dateFormat($date); - } - - // Display the date using theme datetime. - // @todo How should RDFa attributes be added to this? - $elements[$delta] = array( - '#theme' => 'datetime', - '#text' => $formatted_date, - '#html' => FALSE, - '#attributes' => array( - 'datetime' => $iso_date, - 'property' => array('dc:date'), - 'datatype' => 'xsd:dateTime', - ), - ); - } - - return $elements; - - } - - /** - * Creates a formatted date value as a string. - * - * @param object $date - * A date object. - * - * @return string - * A formatted date string using the chosen format. - */ - function dateFormat($date) { - $format_type = $this->getSetting('format_type'); - return $this->dateService->format($date->getTimestamp(), $format_type); - } - - /** - * {@inheritdoc} - */ - public function settingsForm(array $form, array &$form_state) { - $time = new DrupalDateTime(); - $format_types = $this->dateStorage->loadMultiple(); - foreach ($format_types as $type => $type_info) { - $format = $this->dateService->format($time->format('U'), $type); - $options[$type] = $type_info->label() . ' (' . $format . ')'; - } - - $elements['format_type'] = array( - '#type' => 'select', - '#title' => t('Date format'), - '#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."), - '#options' => $options, - '#default_value' => $this->getSetting('format_type'), - ); - - return $elements; - } - - /** - * {@inheritdoc} - */ - public function settingsSummary() { - $summary = array(); - $date = new DrupalDateTime(); - $summary[] = t('Format: @display', array('@display' => $this->dateFormat($date, FALSE))); - return $summary; - } - -}