How would I apply a transition to a State Machine field? I am trying to transition a state_machine field on the User entity during an event. What I have thus far is:

/* @var \Drupal\user\Entity\User $user */
$user = User::load($id);

/* @var \Drupal\Core\Field\FieldItemListInterface $state_field */
$state_field = $user-&gt;get('field_screening_status');

/* @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state */
$state = $state_field-&gt;get(0);

$transitions = $state-&gt;getTransitions();

$state-&gt;applyTransition($transitions['mark_needs_screening']);


Of course, this does not actually save it to the user. I would actually need to call:

$user-&gt;set('field_screening_status', 'needs_screening');
$user-&gt;save();


But, how might I instead use the state_machine API to apply the transition to a user field?
Here's what I ended up with. Would be curious if the community has a better solution.

/* @var \Drupal\user\Entity\User $user */
$user = User::load(\Drupal::currentUser());

/* @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state */
$state = $user-&gt;get('field_screening_status')-&gt;get(0);

$transitions = $state-&gt;getTransitions();

if (array_key_exists('mark_needs_screening', $transitions)) {
  $user-&gt;set('field_screening_status', 'needs_screening');
  $user-&gt;save();
}


UPDATE: Thanks to @lisastreeter in #commerce on drupal.slack.com

$user = User::load($id);
$state = $user-&gt;get('field_screening_status')-&gt;first();
$transition = $state-&gt;getWorkflow()-&gt;getTransition('mark_needs_screening');

if ($transition) {
  $state-&gt;applyTransition($transition);
}


Not sure if that get(0) change to first() really made the difference, but it works now.