updated core to 8.6.3
This commit is contained in:
+3
-148
@@ -2,17 +2,9 @@
|
||||
|
||||
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
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 Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampAgoFormatter;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Time ago' formatter for 'datetime' fields.
|
||||
@@ -25,80 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The date formatter service.
|
||||
*
|
||||
* @var \Drupal\Core\Datetime\DateFormatterInterface
|
||||
*/
|
||||
protected $dateFormatter;
|
||||
|
||||
/**
|
||||
* The current Request object.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Constructs a DateTimeTimeAgoFormatter object.
|
||||
*
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the formatter.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
||||
* The definition of the field to which the formatter is associated.
|
||||
* @param array $settings
|
||||
* The formatter settings.
|
||||
* @param string $label
|
||||
* The formatter label display setting.
|
||||
* @param string $view_mode
|
||||
* The view mode.
|
||||
* @param array $third_party_settings
|
||||
* Third party settings.
|
||||
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
||||
* The date formatter service.
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The current request.
|
||||
*/
|
||||
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter, Request $request) {
|
||||
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
|
||||
|
||||
$this->dateFormatter = $date_formatter;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
$settings = [
|
||||
'future_format' => '@interval hence',
|
||||
'past_format' => '@interval ago',
|
||||
'granularity' => 2,
|
||||
] + parent::defaultSettings();
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$configuration['field_definition'],
|
||||
$configuration['settings'],
|
||||
$configuration['label'],
|
||||
$configuration['view_mode'],
|
||||
$configuration['third_party_settings'],
|
||||
$container->get('date.formatter'),
|
||||
$container->get('request_stack')->getCurrentRequest()
|
||||
);
|
||||
}
|
||||
class DateTimeTimeAgoFormatter extends TimestampAgoFormatter {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -118,50 +37,6 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::settingsForm($form, $form_state);
|
||||
|
||||
$form['future_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Future format'),
|
||||
'#default_value' => $this->getSetting('future_format'),
|
||||
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
|
||||
];
|
||||
|
||||
$form['past_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Past format'),
|
||||
'#default_value' => $this->getSetting('past_format'),
|
||||
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
|
||||
];
|
||||
|
||||
$form['granularity'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Granularity'),
|
||||
'#default_value' => $this->getSetting('granularity'),
|
||||
'#description' => $this->t('How many time units should be shown in the formatted output.'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = parent::settingsSummary();
|
||||
|
||||
$future_date = new DrupalDateTime('1 year 1 month 1 week 1 day 1 hour 1 minute');
|
||||
$past_date = new DrupalDateTime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
|
||||
$summary[] = t('Future date: %display', ['%display' => $this->formatDate($future_date)]);
|
||||
$summary[] = t('Past date: %display', ['%display' => $this->formatDate($past_date)]);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date/time as a time interval.
|
||||
*
|
||||
@@ -172,27 +47,7 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
||||
* The formatted date/time string using the past or future format setting.
|
||||
*/
|
||||
protected function formatDate(DrupalDateTime $date) {
|
||||
$granularity = $this->getSetting('granularity');
|
||||
$timestamp = $date->getTimestamp();
|
||||
$options = [
|
||||
'granularity' => $granularity,
|
||||
'return_as_object' => TRUE,
|
||||
];
|
||||
|
||||
if ($this->request->server->get('REQUEST_TIME') > $timestamp) {
|
||||
$result = $this->dateFormatter->formatTimeDiffSince($timestamp, $options);
|
||||
$build = [
|
||||
'#markup' => new FormattableMarkup($this->getSetting('past_format'), ['@interval' => $result->getString()]),
|
||||
];
|
||||
}
|
||||
else {
|
||||
$result = $this->dateFormatter->formatTimeDiffUntil($timestamp, $options);
|
||||
$build = [
|
||||
'#markup' => new FormattableMarkup($this->getSetting('future_format'), ['@interval' => $result->getString()]),
|
||||
];
|
||||
}
|
||||
CacheableMetadata::createFromObject($result)->applyTo($build);
|
||||
return $build;
|
||||
return parent::formatTimestamp($date->getTimestamp());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* The request stack used to determin current time.
|
||||
* The request stack used to determine current time.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\RequestStack
|
||||
*/
|
||||
|
||||
@@ -171,7 +171,7 @@ class DateTimeFieldTest extends DateTestBase {
|
||||
// past. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME - 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
@@ -201,7 +201,7 @@ class DateTimeFieldTest extends DateTestBase {
|
||||
// future. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME + 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
@@ -323,7 +323,7 @@ class DateTimeFieldTest extends DateTestBase {
|
||||
// past. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME - 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
@@ -350,7 +350,7 @@ class DateTimeFieldTest extends DateTestBase {
|
||||
// future. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME + 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Functional;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests the functionality of DateTimeTimeAgoFormatter field formatter.
|
||||
*
|
||||
* @group field
|
||||
*/
|
||||
class DateTimeTimeAgoFormatterTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* An array of display options to pass to entity_get_display().
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $displayOptions;
|
||||
|
||||
/**
|
||||
* A field storage to use in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldStorageConfig
|
||||
*/
|
||||
protected $fieldStorage;
|
||||
|
||||
/**
|
||||
* The field used in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldConfig
|
||||
*/
|
||||
protected $field;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['datetime', 'entity_test', 'field_ui'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$web_user = $this->drupalCreateUser([
|
||||
'access administration pages',
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
'administer entity_test fields',
|
||||
'administer entity_test display',
|
||||
'administer entity_test form display',
|
||||
'view the administration theme',
|
||||
]);
|
||||
$this->drupalLogin($web_user);
|
||||
|
||||
$field_name = 'field_datetime';
|
||||
$type = 'datetime';
|
||||
$widget_type = 'datetime_default';
|
||||
$formatter_type = 'datetime_time_ago';
|
||||
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $type,
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'required' => TRUE,
|
||||
]);
|
||||
$this->field->save();
|
||||
|
||||
EntityFormDisplay::load('entity_test.entity_test.default')
|
||||
->setComponent($field_name, ['type' => $widget_type])
|
||||
->save();
|
||||
|
||||
$this->displayOptions = [
|
||||
'type' => $formatter_type,
|
||||
'label' => 'hidden',
|
||||
];
|
||||
|
||||
EntityViewDisplay::create([
|
||||
'targetEntityType' => $this->field->getTargetEntityTypeId(),
|
||||
'bundle' => $this->field->getTargetBundle(),
|
||||
'mode' => 'full',
|
||||
'status' => TRUE,
|
||||
])->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the formatter settings.
|
||||
*/
|
||||
public function testSettings() {
|
||||
$this->drupalGet('entity_test/structure/entity_test/display');
|
||||
|
||||
$edit = [
|
||||
'fields[field_datetime][region]' => 'content',
|
||||
'fields[field_datetime][type]' => 'datetime_time_ago',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
|
||||
$this->drupalPostForm(NULL, [], 'field_datetime_settings_edit');
|
||||
$edit = [
|
||||
'fields[field_datetime][settings_edit_form][settings][future_format]' => 'ends in @interval',
|
||||
'fields[field_datetime][settings_edit_form][settings][past_format]' => 'started @interval ago',
|
||||
'fields[field_datetime][settings_edit_form][settings][granularity]' => 3,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, 'Update');
|
||||
$this->drupalPostForm(NULL, [], 'Save');
|
||||
|
||||
$this->assertSession()->pageTextContains('ends in 1 year 1 month 1 week');
|
||||
$this->assertSession()->pageTextContains('started 1 year 1 month 1 week ago');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user