I had assumed that I could change an entity's state like this:

      $license->state = 'active';
      $license->save();

That appears to work, but I don't know if I'm bypassing things like the transition guards.

I see in the code for Commerce Shipping that there's this:

      $transition = $shipment->getState()->getWorkflow()->getTransition('finalize');
      $shipment->getState()->applyTransition($transition);
      $shipment->save();

Is that correct, or are they both equivalent?

If the latter method is correct and my method is wrong, then the README should document this.

Comments

joachim created an issue. See original summary.

bojanz’s picture

Title: README should document API to change an entity's state » Document how an entity's state should be changed
Category: Bug report » Task

Need to do #3012846: Add StateItem::applyTransitionById() first.

It's generally recommended to apply the transition instead of hardcoding the ID, so that you can allow a different workflow to use a different state for the same transition (placed orders end up in different states, for example)

  • bojanz committed d2750e7 on 8.x-1.x
    Issue #2900318 by bojanz: Document how an entity's state should be...
bojanz’s picture

Status: Active » Fixed

Done.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.