Commit 31f9ebf7 authored by catch's avatar catch

Issue #3008446 by alexpott, jcnventura, fenstrat, quietone, voleger, andypost:...

Issue #3008446 by alexpott, jcnventura, fenstrat, quietone, voleger, andypost: Complete the deprecation of format_date()
parent 4bbebca9
......@@ -329,6 +329,7 @@ function format_size($size, $langcode = NULL) {
* @see https://www.drupal.org/node/1876852
*/
function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
@trigger_error("format_date() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::service('date.formatter')->format() instead. See https://www.drupal.org/node/1876852", E_USER_DEPRECATED);
return \Drupal::service('date.formatter')->format($timestamp, $type, $format, $timezone, $langcode);
}
......
......@@ -507,17 +507,19 @@ function theme_settings_convert_to_config(array $theme_settings, Config $config)
* - text:
*/
function template_preprocess_time(&$variables) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
// Format the 'datetime' attribute based on the timestamp.
// @see http://www.w3.org/TR/html5-author/the-time-element.html#attr-time-datetime
if (!isset($variables['attributes']['datetime']) && isset($variables['timestamp'])) {
$variables['attributes']['datetime'] = format_date($variables['timestamp'], 'html_datetime', '', 'UTC');
$variables['attributes']['datetime'] = $date_formatter->format($variables['timestamp'], 'html_datetime', '', 'UTC');
}
// If no text was provided, try to auto-generate it.
if (!isset($variables['text'])) {
// Format and use a human-readable version of the timestamp, if any.
if (isset($variables['timestamp'])) {
$variables['text'] = format_date($variables['timestamp']);
$variables['text'] = $date_formatter->format($variables['timestamp']);
}
// Otherwise, use the literal datetime attribute.
elseif (isset($variables['attributes']['datetime'])) {
......
......@@ -147,11 +147,11 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
* - #date_date_format: A date format string that describes the format that
* should be displayed to the end user for the date. When using HTML5
* elements the format MUST use the appropriate HTML5 format for that
* element, no other format will work. See the format_date() function for a
* list of the possible formats and HTML5 standards for the HTML5
* requirements. Defaults to the right HTML5 format for the chosen element
* if a HTML5 element is used, otherwise defaults to
* DateFormat::load('html_date')->getPattern().
* element, no other format will work. See the
* DateFormatterInterface::format() function for a list of the possible
* formats and HTML5 standards for the HTML5 requirements. Defaults to the
* right HTML5 format for the chosen element if a HTML5 element is used,
* otherwise defaults to DateFormat::load('html_date')->getPattern().
* - #date_date_element: The date element. Options are:
* - datetime: Use the HTML5 datetime element type.
* - datetime-local: Use the HTML5 datetime-local element type.
......@@ -167,11 +167,11 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
* - #date_time_format: A date format string that describes the format that
* should be displayed to the end user for the time. When using HTML5
* elements the format MUST use the appropriate HTML5 format for that
* element, no other format will work. See the format_date() function for
* a list of the possible formats and HTML5 standards for the HTML5
* requirements. Defaults to the right HTML5 format for the chosen element
* if a HTML5 element is used, otherwise defaults to
* DateFormat::load('html_time')->getPattern().
* element, no other format will work. See the
* DateFormatterInterface::format() function for a list of the possible
* formats and HTML5 standards for the HTML5 requirements. Defaults to the
* right HTML5 format for the chosen element if a HTML5 element is used,
* otherwise defaults to DateFormat::load('html_time')->getPattern().
* - #date_time_callbacks: An array of optional callbacks for the time
* element. Can be used to add a jQuery timepicker or an 'All day' checkbox.
* - #date_year_range: A description of the range of years to allow, like
......@@ -212,6 +212,8 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
*
* @return array
* The form element whose value has been processed.
*
* @see \Drupal\Core\Datetime\DateFormatterInterface::format()
*/
public static function processDatetime(&$element, FormStateInterface $form_state, &$complete_form) {
$format_settings = [];
......
......@@ -31,8 +31,8 @@ public function __construct($environment, $class_loader, $allow_dumping = FALSE,
// Prime the module list and corresponding Extension objects.
// @todo Remove System module. Needed because
// \Drupal\Core\Datetime\DateFormatter has a (needless) dependency on the
// 'date_format' entity, so calls to format_date()/format_interval() cause
// a plugin not found exception.
// 'date_format' entity, so calls to DateFormatter::format() and
// DateFormatter::formatInterval() cause a plugin not found exception.
$this->moduleList = [
'system' => 0,
'simpletest' => 0,
......
......@@ -111,7 +111,7 @@ function hook_tokens($type, $tokens, array $data, array $options, \Drupal\Core\R
break;
case 'created':
$replacements[$original] = format_date($node->getCreatedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
}
}
......
......@@ -623,6 +623,8 @@ function comment_preprocess_block(&$variables) {
* Array keys: #comment, #commented_entity.
*/
function template_preprocess_comment(&$variables) {
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
/** @var \Drupal\comment\CommentInterface $comment */
$comment = $variables['elements']['#comment'];
$commented_entity = $comment->getCommentedEntity();
......@@ -638,13 +640,13 @@ function template_preprocess_comment(&$variables) {
$variables['author'] = \Drupal::service('renderer')->render($username);
$variables['author_id'] = $comment->getOwnerId();
$variables['new_indicator_timestamp'] = $comment->getChangedTime();
$variables['created'] = format_date($comment->getCreatedTime());
// Avoid calling format_date() twice on the same timestamp.
$variables['created'] = $date_formatter->format($comment->getCreatedTime());
// Avoid calling DateFormatterInterface::format() twice on the same timestamp.
if ($comment->getChangedTime() == $comment->getCreatedTime()) {
$variables['changed'] = $variables['created'];
}
else {
$variables['changed'] = format_date($comment->getChangedTime());
$variables['changed'] = $date_formatter->format($comment->getChangedTime());
}
if (theme_get_setting('features.comment_user_picture')) {
......@@ -682,13 +684,13 @@ function template_preprocess_comment(&$variables) {
'#account' => $account_parent,
];
$variables['parent_author'] = \Drupal::service('renderer')->render($username);
$variables['parent_created'] = format_date($comment_parent->getCreatedTime());
// Avoid calling format_date() twice on the same timestamp.
$variables['parent_created'] = $date_formatter->format($comment_parent->getCreatedTime());
// Avoid calling DateFormatterInterface::format() twice on same timestamp.
if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) {
$variables['parent_changed'] = $variables['parent_created'];
}
else {
$variables['parent_changed'] = format_date($comment_parent->getChangedTime());
$variables['parent_changed'] = $date_formatter->format($comment_parent->getChangedTime());
}
$permalink_uri_parent = $comment_parent->permalink();
$attributes = $permalink_uri_parent->getOption('attributes') ?: [];
......
......@@ -209,13 +209,13 @@ function comment_tokens($type, $tokens, array $data, array $options, BubbleableM
case 'created':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = format_date($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = format_date($comment->getChangedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($comment->getChangedTime(), 'medium', '', NULL, $langcode);
break;
case 'entity':
......
......@@ -143,7 +143,7 @@ public function testCommentEditPreviewSave() {
$edit['date[date]'] = $date->format('Y-m-d');
$edit['date[time]'] = $date->format('H:i:s');
$raw_date = $date->getTimestamp();
$expected_text_date = format_date($raw_date);
$expected_text_date = $this->container->get('date.formatter')->format($raw_date);
$expected_form_date = $date->format('Y-m-d');
$expected_form_time = $date->format('H:i:s');
$comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
......
......@@ -162,10 +162,12 @@ protected function doTestAuthoringInfo() {
'uid' => $user->id(),
'created' => REQUEST_TIME - mt_rand(0, 1000),
];
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
$edit = [
'uid' => $user->getAccountName() . ' (' . $user->id() . ')',
'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
'date[date]' => $date_formatter->format($values[$langcode]['created'], 'custom', 'Y-m-d'),
'date[time]' => $date_formatter->format($values[$langcode]['created'], 'custom', 'H:i:s'),
];
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
}
......
......@@ -495,7 +495,7 @@ public function testDateFormatTranslation() {
// Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
// display "Tue".
$formatted_date = format_date(494015820, $id, NULL, 'America/New_York', 'fr');
$formatted_date = $this->container->get('date.formatter')->format(494015820, $id, NULL, 'America/New_York', 'fr');
$this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
}
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\content_translation;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityChangesDetectionTrait;
......@@ -90,6 +91,13 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E
*/
protected $messenger;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Initializes an instance of the content translation controller.
*
......@@ -105,8 +113,10 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E
* The current user.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityManagerInterface $entity_manager, AccountInterface $current_user, MessengerInterface $messenger) {
public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityManagerInterface $entity_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter) {
$this->entityTypeId = $entity_type->id();
$this->entityType = $entity_type;
$this->languageManager = $language_manager;
......@@ -115,6 +125,7 @@ public function __construct(EntityTypeInterface $entity_type, LanguageManagerInt
$this->currentUser = $current_user;
$this->fieldStorageDefinitions = $entity_manager->getLastInstalledFieldStorageDefinitions($this->entityTypeId);
$this->messenger = $messenger;
$this->dateFormatter = $date_formatter;
}
/**
......@@ -127,7 +138,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
$container->get('content_translation.manager'),
$container->get('entity.manager'),
$container->get('current_user'),
$container->get('messenger')
$container->get('messenger'),
$container->get('date.formatter')
);
}
......@@ -508,8 +520,11 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En
'#type' => 'textfield',
'#title' => t('Authored on'),
'#maxlength' => 25,
'#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', ['%time' => format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O'), '%timezone' => format_date(REQUEST_TIME, 'custom', 'O')]),
'#default_value' => $new_translation || !$date ? '' : format_date($date, 'custom', 'Y-m-d H:i:s O'),
'#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', [
'%time' => $this->dateFormatter->format(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O'),
'%timezone' => $this->dateFormatter->format(REQUEST_TIME, 'custom', 'O'),
]),
'#default_value' => $new_translation || !$date ? '' : $this->dateFormatter->format($date, 'custom', 'Y-m-d H:i:s O'),
];
$form['#process'][] = [$this, 'entityFormSharedElements'];
......
......@@ -329,7 +329,7 @@ protected function doTestAuthoringInfo() {
];
$edit = [
'content_translation[uid]' => $user->getAccountName(),
'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
'content_translation[created]' => $this->container->get('date.formatter')->format($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
];
$url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
......
......@@ -322,7 +322,7 @@ protected function doTestAuthoringInfo() {
];
$edit = [
'content_translation[uid]' => $user->getAccountName(),
'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
'content_translation[created]' => $this->container->get('date.formatter')->format($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
];
$url = $entity->toUrl('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
......
......@@ -2,6 +2,7 @@
namespace Drupal\datetime\Plugin\views\argument;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\views\FieldAPIHandlerTrait;
......@@ -37,8 +38,8 @@ class Date extends NumericDate {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match);
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, DateFormatterInterface $date_formatter) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match, $date_formatter);
$definition = $this->getFieldStorageDefinition();
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
......
......@@ -113,8 +113,10 @@ public function testDateField() {
case 'format_type':
// Verify that a date is displayed. Since this is a date-only
// field, it is expected to display the time as 00:00:00.
$expected = format_date($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
$expected = $date_formatter->format($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
$expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
$output = $this->renderTestEntity($id);
$expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
$this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
......@@ -279,8 +281,9 @@ public function testDatetimeField() {
switch ($setting) {
case 'format_type':
// Verify that a date is displayed.
$expected = format_date($date->getTimestamp(), $new_value);
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$date_formatter = $this->container->get('date.formatter');
$expected = $date_formatter->format($date->getTimestamp(), $new_value);
$expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$output = $this->renderTestEntity($id);
$expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
$this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
......
......@@ -1179,13 +1179,13 @@ function file_tokens($type, $tokens, array $data, array $options, BubbleableMeta
case 'created':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = format_date($file->getCreatedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($file->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$date_format = DateFormat::load('medium');
$bubbleable_metadata = $bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = format_date($file->getChangedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($file->getChangedTime(), 'medium', '', NULL, $langcode);
break;
case 'owner':
......
......@@ -21,6 +21,8 @@ public function testFileTokenReplacement() {
$node_storage = $this->container->get('entity.manager')->getStorage('node');
$token_service = \Drupal::token();
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
// Create file field.
$type_name = 'article';
......@@ -48,10 +50,10 @@ public function testFileTokenReplacement() {
$tests['[file:mime]'] = Html::escape($file->getMimeType());
$tests['[file:size]'] = format_size($file->getSize());
$tests['[file:url]'] = Html::escape(file_create_url($file->getFileUri()));
$tests['[file:created]'] = format_date($file->getCreatedTime(), 'medium', '', NULL, $language_interface->getId());
$tests['[file:created:short]'] = format_date($file->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
$tests['[file:changed]'] = format_date($file->getChangedTime(), 'medium', '', NULL, $language_interface->getId());
$tests['[file:changed:short]'] = format_date($file->getChangedTime(), 'short', '', NULL, $language_interface->getId());
$tests['[file:created]'] = $date_formatter->format($file->getCreatedTime(), 'medium', '', NULL, $language_interface->getId());
$tests['[file:created:short]'] = $date_formatter->format($file->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
$tests['[file:changed]'] = $date_formatter->format($file->getChangedTime(), 'medium', '', NULL, $language_interface->getId());
$tests['[file:changed:short]'] = $date_formatter->format($file->getChangedTime(), 'short', '', NULL, $language_interface->getId());
$tests['[file:owner]'] = Html::escape($this->adminUser->getDisplayName());
$tests['[file:owner:uid]'] = $file->getOwnerId();
......
......@@ -112,7 +112,7 @@ public function testConfigTranslation() {
// Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
// display "Tue".
$formatted_date = format_date(494015820, $type = 'medium', NULL, 'America/New_York', $this->langcode);
$formatted_date = $this->container->get('date.formatter')->format(494015820, $type = 'medium', NULL, 'America/New_York', $this->langcode);
$this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
// Assert strings from image module config are not available.
......
......@@ -111,7 +111,10 @@ public function testInterface() {
// Check if translations are available for Drupal core.
$this->drupalGet('admin/reports/translations');
$this->assertText(t('Updates for: @project', ['@project' => t('Drupal core')]), 'Translations found');
$this->assertText(new FormattableMarkup('@module (@date)', ['@module' => t('Drupal core'), '@date' => format_date(REQUEST_TIME, 'html_date')]), 'Core translation update');
$this->assertText(new FormattableMarkup('@module (@date)', [
'@module' => t('Drupal core'),
'@date' => $this->container->get('date.formatter')->format(REQUEST_TIME, 'html_date'),
]), 'Core translation update');
$update_button = $this->xpath('//input[@type="submit"][@value="' . t('Update translations') . '"]');
$this->assertTrue($update_button, 'Update translations button');
}
......
......@@ -136,8 +136,10 @@ public function testUpdateImportSourceRemote() {
// Check the status on the Available translation status page.
$this->assertRaw('<label for="edit-langcodes-de" class="visually-hidden">Update German</label>', 'German language found');
$this->assertText('Updates for: Contributed module one, Contributed module two, Custom module one, Locale test', 'Updates found');
$this->assertText('Contributed module one (' . format_date($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module one');
$this->assertText('Contributed module two (' . format_date($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module two');
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
$this->assertText('Contributed module one (' . $date_formatter->format($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module one');
$this->assertText('Contributed module two (' . $date_formatter->format($this->timestampNew, 'html_date') . ')', 'Updates for Contrib module two');
// Execute the translation update.
$this->drupalPostForm('admin/reports/translations', [], t('Update translations'));
......
......@@ -183,13 +183,13 @@ function node_tokens($type, $tokens, array $data, array $options, BubbleableMeta
case 'created':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = format_date($node->getCreatedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'medium', '', NULL, $langcode);
break;
case 'changed':
$date_format = DateFormat::load('medium');
$bubbleable_metadata->addCacheableDependency($date_format);
$replacements[$original] = format_date($node->getChangedTime(), 'medium', '', NULL, $langcode);
$replacements[$original] = \Drupal::service('date.formatter')->format($node->getChangedTime(), 'medium', '', NULL, $langcode);
break;
}
}
......
......@@ -144,7 +144,7 @@ public function revisionShow($node_revision) {
*/
public function revisionPageTitle($node_revision) {
$node = $this->entityManager()->getStorage('node')->loadRevision($node_revision);
return $this->t('Revision of %title from %date', ['%title' => $node->label(), '%date' => format_date($node->getRevisionCreationTime())]);
return $this->t('Revision of %title from %date', ['%title' => $node->label(), '%date' => $this->dateFormatter->format($node->getRevisionCreationTime())]);
}
/**
......
......@@ -3,6 +3,7 @@
namespace Drupal\node\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -44,6 +45,13 @@ class NodeRevisionDeleteForm extends ConfirmFormBase {
*/
protected $connection;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a new NodeRevisionDeleteForm.
*
......@@ -53,11 +61,14 @@ class NodeRevisionDeleteForm extends ConfirmFormBase {
* The node type storage.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityStorageInterface $node_storage, EntityStorageInterface $node_type_storage, Connection $connection) {
public function __construct(EntityStorageInterface $node_storage, EntityStorageInterface $node_type_storage, Connection $connection, DateFormatterInterface $date_formatter) {
$this->nodeStorage = $node_storage;
$this->nodeTypeStorage = $node_type_storage;
$this->connection = $connection;
$this->dateFormatter = $date_formatter;
}
/**
......@@ -68,7 +79,8 @@ public static function create(ContainerInterface $container) {
return new static(
$entity_manager->getStorage('node'),
$entity_manager->getStorage('node_type'),
$container->get('database')
$container->get('database'),
$container->get('date.formatter')
);
}
......@@ -83,7 +95,9 @@ public function getFormId() {
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete the revision from %revision-date?', ['%revision-date' => format_date($this->revision->getRevisionCreationTime())]);
return t('Are you sure you want to delete the revision from %revision-date?', [
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
]);
}
/**
......@@ -120,7 +134,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$node_type = $this->nodeTypeStorage->load($this->revision->bundle())->label();
$this->messenger()
->addStatus($this->t('Revision from %revision-date of @type %title has been deleted.', [
'%revision-date' => format_date($this->revision->getRevisionCreationTime()),
'%revision-date' => $this->dateFormatter->format($this->revision->getRevisionCreationTime()),
'@type' => $node_type,
'%title' => $this->revision->label(),
]));
......
......@@ -3,6 +3,7 @@
namespace Drupal\node;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
......@@ -32,6 +33,13 @@ class NodeForm extends ContentEntityForm {
*/
protected $currentUser;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a NodeForm object.
*
......@@ -45,11 +53,14 @@ class NodeForm extends ContentEntityForm {
* The time service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->tempStoreFactory = $temp_store_factory;
$this->currentUser = $current_user;
$this->dateFormatter = $date_formatter;
}
/**
......@@ -61,7 +72,8 @@ public static function create(ContainerInterface $container) {
$container->get('tempstore.private'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('current_user')
$container->get('current_user'),
$container->get('date.formatter')
);
}
......@@ -134,7 +146,7 @@ public function form(array $form, FormStateInterface $form_state) {
$form['meta']['changed'] = [
'#type' => 'item',
'#title' => $this->t('Last saved'),
'#markup' => !$node->isNew() ? format_date($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
'#markup' => !$node->isNew() ? $this->dateFormatter->format($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
'#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
];
$form['meta']['author'] = [
......
......@@ -63,7 +63,7 @@ public function entityFormEntityBuild($entity_type, EntityInterface $entity, arr
$translation['status'] = $entity->isPublished();
$account = $entity->uid->entity;
$translation['uid'] = $account ? $account->id() : 0;
$translation['created'] = format_date($entity->created->value, 'custom', 'Y-m-d H:i:s O');
$translation['created'] = $this->dateFormatter->format($entity->created->value, 'custom', 'Y-m-d H:i:s O');
}
parent::entityFormEntityBuild($entity_type, $entity, $form, $form_state);
}
......
......@@ -61,7 +61,7 @@ public function testNodeCreation() {
// Verify that pages do not show submitted information by default.
$this->drupalGet('node/' . $node->id());
$this->assertNoText($node->getOwner()->getAccountName());
$this->assertNoText(format_date($node->getCreatedTime()));
$this->assertNoText($this->container->get('date.formatter')->format($node->getCreatedTime()));
// Change the node type setting to show submitted by information.
/** @var \Drupal\node\NodeTypeInterface $node_type */
......@@ -71,7 +71,7 @@ public function testNodeCreation() {
$this->drupalGet('node/' . $node->id());
$this->assertText($node->getOwner()->getAccountName());
$this->assertText(format_date($node->getCreatedTime()));
$this->assertText($this->container->get('date.formatter')->format($node->getCreatedTime()));
// Check if the node revision checkbox is not rendered on node creation form.
$admin_user = $this->drupalCreateUser(['administer nodes', 'create page content']);
......
......@@ -217,7 +217,7 @@ public function testNodeMetaInformation() {
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->drupalGet("node/" . $node->id() . "/edit");
$this->assertNoText('Published');
$this->assertNoText(format_date($node->getChangedTime(), 'short'));
$this->assertNoText($this->container->get('date.formatter')->format($node->getChangedTime(), 'short'));
// Check that users with the 'administer nodes' permission can see the meta
// information.
......@@ -233,7 +233,7 @@ public function testNodeMetaInformation() {
$node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
$this->drupalGet("node/" . $node->id() . "/edit");
$this->assertText('Published');
$this->assertText(format_date($node->getChangedTime(), 'short'));
$this->assertText($this->container->get('date.formatter')->format($node->getChangedTime(), 'short'));
}
/**
......
......@@ -148,7 +148,7 @@ public function testRevisions() {
[
'@type' => 'Basic page',
'%title' => $nodes[1]->getTitle(),
'%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
'%revision-date' => $this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime()),
]),
'Revision reverted.');
$node_storage->resetCache([$node->id()]);
......@@ -173,7 +173,7 @@ public function testRevisions() {
$this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
$this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',
[
'%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
'%revision-date' => $this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime()),
'@type' => 'Basic page',
'%title' => $nodes[1]->getTitle(),
]),
......@@ -196,7 +196,7 @@ public function testRevisions() {
$this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
'@type' => 'Basic page',
'%title' => $nodes[2]->getTitle(),
'%revision-date' => format_date($old_revision_date),
'%revision-date' => $this->container->get('date.formatter')->format($old_revision_date),
]));
// Create 50 more revisions in order to trigger paging on the revisions
......
......@@ -164,7 +164,7 @@ public function testRevisions() {
$this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
'@type' => 'Basic page',
'%title' => $nodes[1]->label(),
'%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
'%revision-date' => $this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime()),
]), 'Revision reverted.');
$node_storage->resetCache([$node->id()]);
$reverted_node = $node_storage->load($node->id());
......@@ -181,7 +181,7 @@ public function testRevisions() {
// Confirm revisions delete properly.
$this->drupalPostForm("node/" . $node->id() . "/revisions/" . $nodes[1]->getRevisionId() . "/delete", [], t('Delete'));
$this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.', [
'%revision-date' => format_date($nodes[1]->getRevisionCreationTime()),
'%revision-date' => $this->container->get('date.formatter')->format($nodes[1]->getRevisionCreationTime()),
'@type' => 'Basic page',
'%title' => $nodes[1]->label(),
]), 'Revision deleted.');
......@@ -202,7 +202,7 @@ public function testRevisions() {
$this->assertRaw(t('@type %title has been reverted to the revision from %revision-date.', [
'@type' => 'Basic page',
'%title' => $nodes[2]->label(),
'%revision-date' => format_date($old_revision_date),
'%revision-date' => $this->container->get('date.formatter')->format($old_revision_date),
]));
// Make a new revision and set it to not be default.
......
......@@ -112,12 +112,12 @@ public function testNodeRevisionDoubleEscapeFix() {
$this->drupalGet('node/' . $node->id() . '/revisions');
// Assert the old revision message.
$date = format_date($nodes[0]->revision_timestamp->value, 'short');
$date = $this->container->get('date.formatter')->format($nodes[0]->revision_timestamp->value, 'short');
$url = new Url('entity.node.revision', ['node' => $nodes[0]->id(), 'node_revision' => $nodes[0]->getRevisionId()]);
$this->assertRaw(\Drupal::l($date, $url) . ' by ' . $editor);