diff --git a/radioactivity.services.yml b/radioactivity.services.yml index 38ec0a6..9d78e93 100644 --- a/radioactivity.services.yml +++ b/radioactivity.services.yml @@ -1,7 +1,7 @@ services: radioactivity.processor: class: Drupal\radioactivity\RadioactivityProcessor - arguments: ["@entity_type.manager", "@state", "@logger.factory", "@radioactivity.storage"] + arguments: ["@entity_type.manager", "@state", "@logger.factory", "@radioactivity.storage", "@datetime:time"] radioactivity.storage: diff --git a/src/Plugin/Field/FieldType/RadioactivityField.php b/src/Plugin/Field/FieldType/RadioactivityField.php index 18965f4..b105b69 100644 --- a/src/Plugin/Field/FieldType/RadioactivityField.php +++ b/src/Plugin/Field/FieldType/RadioactivityField.php @@ -102,7 +102,7 @@ class RadioactivityField extends FieldItemBase { */ public static function generateSampleValue(FieldDefinitionInterface $field_definition) { $values['energy'] = 1; - $values['timestamp'] = REQUEST_TIME; + $values['timestamp'] = \Drupal::time()->getRequestTime(); return $values; } @@ -177,7 +177,7 @@ Decay: Energy increases by the emission amount. Decreases 50% per half-life time if (!$this->energy) { $this->energy = 0; } - $this->timestamp = REQUEST_TIME; + $this->timestamp = \Drupal::time()->getRequestTime(); } /** diff --git a/src/Plugin/Field/FieldWidget/RadioactivityEnergy.php b/src/Plugin/Field/FieldWidget/RadioactivityEnergy.php index aabe852..519706e 100644 --- a/src/Plugin/Field/FieldWidget/RadioactivityEnergy.php +++ b/src/Plugin/Field/FieldWidget/RadioactivityEnergy.php @@ -25,7 +25,7 @@ class RadioactivityEnergy extends WidgetBase { public static function defaultSettings() { return [ 'energy' => 0.0, - 'timestamp' => REQUEST_TIME, + 'timestamp' => \Drupal::time()->getRequestTime(), ] + parent::defaultSettings(); } @@ -44,7 +44,7 @@ class RadioactivityEnergy extends WidgetBase { // @todo Should timestamp be configurable? See https://www.drupal.org/node/2677688 // $element['timestamp'] = [ // '#type' => 'hidden', - // '#default_value' => REQUEST_TIME; + // '#default_value' => \Drupal::time()->getRequestTime(); // ]; // Put the form element into the form's "advanced" group. diff --git a/src/RadioactivityProcessor.php b/src/RadioactivityProcessor.php index 00bea2a..c8b931b 100644 --- a/src/RadioactivityProcessor.php +++ b/src/RadioactivityProcessor.php @@ -2,6 +2,7 @@ namespace Drupal\radioactivity; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\State\State; use Drupal\Core\Logger\LoggerChannelFactoryInterface; @@ -43,6 +44,13 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { protected $storage; /** + * The timestamp for the current request. + * + * @var integer + */ + protected $requestTime; + + /** * Constructs a Radioactivity processor. * * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager @@ -52,12 +60,16 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory * The logger. * @param \Drupal\radioactivity\StorageFactory $storage + * The storage factory service. + * @param \Drupal\Component\Datetime\TimeInterface $time + * The time service. */ - public function __construct(EntityTypeManager $entity_type_manager, State $state, LoggerChannelFactoryInterface $logger_factory, StorageFactory $storage) { + public function __construct(EntityTypeManager $entity_type_manager, State $state, LoggerChannelFactoryInterface $logger_factory, StorageFactory $storage, TimeInterface $time) { $this->entityTypeManager = $entity_type_manager; $this->state = $state; $this->log = $logger_factory->get('radioactivity'); $this->storage = $storage->getConfiguredStorage(); + $this->requestTime = $time->getRequestTime(); } /** @@ -75,7 +87,7 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { $last_cron_timestamp = $this->state->get('radioactivity_last_cron_timestamp', 0); - $this->state->set('radioactivity_last_cron_timestamp', REQUEST_TIME); + $this->state->set('radioactivity_last_cron_timestamp', $this->requestTime); foreach ($field_storage_configs as $field_storage) { @@ -113,7 +125,7 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { // seconds have passed since the last cron run. if ($profile == 'decay') { $threshold_timestamp = $last_cron_timestamp - ($last_cron_timestamp % $granularity) + $granularity; - if (REQUEST_TIME < $threshold_timestamp) { + if ($this->requestTime < $threshold_timestamp) { // Granularity not reached yet. return; } @@ -126,7 +138,7 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { // long cron cycle times. Prepare for queue processing. $query = $this->entityTypeManager->getStorage($entity_type)->getQuery() // @todo Why use timestamp in this query? See https://www.drupal.org/node/2677688 - //->condition($field_name . '.timestamp', REQUEST_TIME, ' < ') + //->condition($field_name . '.timestamp', $this->requestTime, ' < ') ->condition($field_name . '.energy', NULL, 'IS NOT NULL'); $nids = $query->execute(); /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */ @@ -136,7 +148,7 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { foreach ($entities as $entity) { $timestamp = $entity->{$field_name}->timestamp; - $elapsed = $timestamp ? REQUEST_TIME - $timestamp : 0; + $elapsed = $timestamp ? $this->requestTime - $timestamp : 0; $energy = $entity->{$field_name}->energy; switch ($profile) { @@ -153,7 +165,7 @@ class RadioactivityProcessor implements RadioactivityProcessorInterface { // Set the new energy level and update the timestamp. $entity->{$field_name}->setValue([ 'energy' => $energy, - 'timestamp' => REQUEST_TIME, + 'timestamp' => $this->requestTime, ]); } else {