In Drupal 8, I am needing to know how to assign/add an entity reference of an e-book to an multivalue entity reference field of the user when an order with the e-book is placed. This will allow the user to then access only the e-books attached to his user account.

What hook do I use? how do I know if there is a product in the order that have field_ebook=true? Then how do add it to the list of entity references?

1 Answer 1


Not a hook but an event.

Orders have an attached workflow. You can listen to the event of an order being placed, see the state_machine module for documentation on this. Basically, you need to define an event subscriber service, subscribe to the event commerce_order.place.post_transition, then check if order is an ebook and if so act based on the order customer ID.

The subscriber method needs like this:

public function orderStateChange(WorkflowTransitionEvent $transition_event) {
  $entity = $transition_event->getEntity();
  // Check the transition id and whetever else you need on the order and then do your thing.
  if ($transition_event->getToState()->getId() == 'completed' && ...) {


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.