commerce-8.x-2.8/modules/order/src/EventSubscriber/TimestampEventSubscriber.php
modules/order/src/EventSubscriber/TimestampEventSubscriber.php
<?php
namespace Drupal\commerce_order\EventSubscriber;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Drupal\Component\Datetime\TimeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TimestampEventSubscriber implements EventSubscriberInterface {
/**
* The time.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a new TimestampEventSubscriber object.
*
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
*/
public function __construct(TimeInterface $time) {
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
'commerce_order.place.pre_transition' => 'onPlaceTransition',
];
return $events;
}
/**
* Sets the order's placed timestamp.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The transition event.
*/
public function onPlaceTransition(WorkflowTransitionEvent $event) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $event->getEntity();
if (empty($order->getPlacedTime())) {
$order->setPlacedTime($this->time->getRequestTime());
}
}
}
