Skip to content
Snippets Groups Projects

Replace REQUEST_TIME in plugins with direct container access

Closed quietone requested to merge issue/drupal-3395986:3395986-replace-requesttime-in into 11.x
Files
8
@@ -2,6 +2,7 @@
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Datetime\DateFormatterInterface;
@@ -49,6 +50,13 @@ class TimestampFormatter extends FormatterBase {
*/
protected $dateFormatStorage;
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a new TimestampFormatter.
*
@@ -70,12 +78,30 @@ class TimestampFormatter extends FormatterBase {
* The date formatter service.
* @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
* The date format storage.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter, EntityStorageInterface $date_format_storage) {
public function __construct(
$plugin_id,
$plugin_definition,
FieldDefinitionInterface $field_definition,
array $settings,
$label,
$view_mode,
array $third_party_settings,
DateFormatterInterface $date_formatter,
EntityStorageInterface $date_format_storage,
?TimeInterface $time = NULL,
) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->dateFormatter = $date_formatter;
$this->dateFormatStorage = $date_format_storage;
if (!$time) {
@trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3395991', E_USER_DEPRECATED);
$time = \Drupal::service('datetime.time');
}
$this->time = $time;
}
/**
@@ -91,7 +117,8 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('date.formatter'),
$container->get('entity_type.manager')->getStorage('date_format')
$container->get('entity_type.manager')->getStorage('date_format'),
$container->get('datetime.time'),
);
}
@@ -124,8 +151,9 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$date_formats = [];
$requestTime = $this->time->getRequestTime();
foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) {
$date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format(REQUEST_TIME, $machine_name)]);
$date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format($requestTime, $machine_name)]);
}
$date_formats[static::CUSTOM_DATE_FORMAT] = $this->t('Custom');
Loading