From 7543cf619a3ff7483cea70cc06ef8ff8e596d5aa Mon Sep 17 00:00:00 2001
From: John Voskuilen <john.voskuilen@sapito.nl>
Date: Mon, 17 Mar 2025 09:27:25 +0100
Subject: [PATCH] Issue #3508337 by haicao-devpanel, johnv, spiderman: Workflow
 doesn't recognize state change first time

---
 src/Entity/WorkflowTargetEntity.php         | 2 +-
 src/Plugin/Field/FieldType/WorkflowItem.php | 6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/Entity/WorkflowTargetEntity.php b/src/Entity/WorkflowTargetEntity.php
index a302de69..617ca885 100644
--- a/src/Entity/WorkflowTargetEntity.php
+++ b/src/Entity/WorkflowTargetEntity.php
@@ -106,7 +106,7 @@ class WorkflowTargetEntity {
    * @return string
    *   The ID of the creation State for the Workflow of the field.
    */
-  private static function getCreationStateId(EntityInterface $entity, $field_name) {
+  public static function getCreationStateId(EntityInterface $entity, $field_name) {
     $sid = '';
 
     /** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity */
diff --git a/src/Plugin/Field/FieldType/WorkflowItem.php b/src/Plugin/Field/FieldType/WorkflowItem.php
index 51a12442..0ff9d269 100644
--- a/src/Plugin/Field/FieldType/WorkflowItem.php
+++ b/src/Plugin/Field/FieldType/WorkflowItem.php
@@ -12,6 +12,7 @@ use Drupal\Core\Url;
 use Drupal\options\Plugin\Field\FieldType\ListItemBase;
 use Drupal\workflow\Entity\Workflow;
 use Drupal\workflow\Entity\WorkflowState;
+use Drupal\workflow\Entity\WorkflowTargetEntity;
 use Drupal\workflow\Entity\WorkflowTransition;
 use Drupal\workflow\Entity\WorkflowTransitionInterface;
 
@@ -285,7 +286,10 @@ class WorkflowItem extends ListItemBase {
     // Create a transition, to pass to the form.
     $entity = $this->getEntity();
     $field_name = $this->getParent()->getName();
-    $current_sid = $this->value;
+    $current_sid = $entity->isNew()
+      ? WorkflowTargetEntity::getCreationStateId($entity, $field_name)
+      : $this->value;
+
     if (!$current_sid) {
       return $transition;
     }
-- 
GitLab