diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php index da2e2a7..5feddcf 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php @@ -2,6 +2,12 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType; +use Drupal\Component\Datetime\TimeInterface; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\TypedData\DataDefinitionInterface; +use Drupal\Core\TypedData\TypedDataInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + /** * Defines the 'created' entity field type. * @@ -14,7 +20,12 @@ * default_formatter = "timestamp" * ) */ -class CreatedItem extends TimestampItem { +class CreatedItem extends TimestampItem implements ContainerFactoryPluginInterface { + + /** + * @var \Drupal\Component\Datetime\TimeInterface + */ + protected $timeService; /** * {@inheritdoc} @@ -22,8 +33,34 @@ class CreatedItem extends TimestampItem { public function applyDefaultValue($notify = TRUE) { parent::applyDefaultValue($notify); // Created fields default to the current timestamp. - $this->setValue(['value' => REQUEST_TIME], $notify); + $this->setValue(['value' => $this->timeService->getRequestTime()], $notify); return $this; } + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('datetime.time') + ); + } + + /** + * CreatedItem constructor. + * + * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition + * @param string $name + * @param \Drupal\Core\TypedData\TypedDataInterface $parent + * @param \Drupal\Component\Datetime\TimeInterface $time_service + */ + public function __construct(DataDefinitionInterface $definition, + $name = NULL, TypedDataInterface $parent = NULL, TimeInterface $time_service) { + parent::__construct($definition, $name, $parent); + $this->timeService = $time_service; + } + }