class TimestampEventSubscriber in Commerce Core 8.2
Hierarchy
- class \Drupal\commerce_order\EventSubscriber\TimestampEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of TimestampEventSubscriber
1 string reference to 'TimestampEventSubscriber'
- commerce_order.services.yml in modules/
order/ commerce_order.services.yml - modules/order/commerce_order.services.yml
1 service uses TimestampEventSubscriber
File
- modules/
order/ src/ EventSubscriber/ TimestampEventSubscriber.php, line 9
Namespace
Drupal\commerce_order\EventSubscriberView source
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',
'commerce_order.pre_transition' => 'onAnyTransition',
];
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());
}
}
/**
* Sets the order's completed timestamp.
*
* @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
* The transition event.
*/
public function onAnyTransition(WorkflowTransitionEvent $event) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $event
->getEntity();
$to_state_id = $event
->getTransition()
->getToState()
->getId();
if ($to_state_id == 'completed' && empty($order
->getCompletedTime())) {
$order
->setCompletedTime($this->time
->getRequestTime());
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TimestampEventSubscriber:: |
protected | property | The time. | |
TimestampEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
TimestampEventSubscriber:: |
public | function | Sets the order's completed timestamp. | |
TimestampEventSubscriber:: |
public | function | Sets the order's placed timestamp. | |
TimestampEventSubscriber:: |
public | function | Constructs a new TimestampEventSubscriber object. |