From 20f5158519f2631d9712a2a0d38fffeaa5391d14 Mon Sep 17 00:00:00 2001
From: John Voskuilen <john.voskuilen@sapito.nl>
Date: Wed, 20 Mar 2024 00:02:50 +0100
Subject: [PATCH] Issue #2926094: WorkflowTransitionElement is called twice on
 a form

---
 src/Form/WorkflowTransitionForm.php                    | 5 ++---
 src/Plugin/Field/FieldWidget/WorkflowDefaultWidget.php | 3 +--
 workflow.form.inc                                      | 9 +++++++--
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/Form/WorkflowTransitionForm.php b/src/Form/WorkflowTransitionForm.php
index 9c919fb9..6ffad7cf 100644
--- a/src/Form/WorkflowTransitionForm.php
+++ b/src/Form/WorkflowTransitionForm.php
@@ -115,10 +115,9 @@ class WorkflowTransitionForm extends ContentEntityForm {
    */
   public function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
     parent::copyFormValuesToEntity($entity, $form, $form_state);
-    // Use a proprietary version of copyFormValuesToEntity(),
-    // passing $entity by reference...
+
+    // Use a proprietary version of copyFormValuesToEntity().
     $values = $form_state->getValues();
-    // ... but only the returning object is OK (!).
     $transition = WorkflowTransitionElement::copyFormValuesToTransition($entity, $form, $form_state, $values);
     return $transition;
   }
diff --git a/src/Plugin/Field/FieldWidget/WorkflowDefaultWidget.php b/src/Plugin/Field/FieldWidget/WorkflowDefaultWidget.php
index f937b45e..5b38d9e0 100644
--- a/src/Plugin/Field/FieldWidget/WorkflowDefaultWidget.php
+++ b/src/Plugin/Field/FieldWidget/WorkflowDefaultWidget.php
@@ -118,8 +118,7 @@ class WorkflowDefaultWidget extends WidgetBase {
     // back to the regular 'value' string format.
     foreach ($values as &$item) {
       if (!empty($item)) {
-        // N.B. Use a proprietary version of copyFormValuesToEntity,
-        // where $entity/$transition is passed by reference.
+        // Use a proprietary version of copyFormValuesToEntity().
         /** @var \Drupal\workflow\Entity\WorkflowTransitionInterface $transition */
         $transition = $item['workflow_transition'];
         $transition = WorkflowTransitionElement::copyFormValuesToTransition($transition, $form, $form_state, $item);
diff --git a/workflow.form.inc b/workflow.form.inc
index 81ea58ef..a913ea01 100644
--- a/workflow.form.inc
+++ b/workflow.form.inc
@@ -220,13 +220,18 @@ function _workflow_transition_form_get_action_buttons(array $form, FormStateInte
  *   A $field_name => $to_sid array.
  */
 function _workflow_transition_form_get_triggering_button(FormStateInterface $form_state) {
-  $result = ['field_name' => '', 'to_sid' => ''];
+  $result = ['field_name' => NULL, 'to_sid' => NULL];
 
   $triggering_element = $form_state->getTriggeringElement();
   if (isset($triggering_element['#workflow'])) {
     $result['field_name'] = $triggering_element['#workflow']['field_name'];
     $result['to_sid'] = $triggering_element['#workflow']['to_sid'];
   }
+  else {
+    $input = $form_state->getUserInput();
+    $result['field_name'] = $input['field_name'] ?? NULL;
+    $result['to_sid'] = $input['to_sid'] ?? NULL;
+  }
 
   return $result;
 }
@@ -240,7 +245,7 @@ function _workflow_transition_form_get_triggering_button(FormStateInterface $for
  *   The element.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The form state.
- * @param arrayt $form
+ * @param array $form
  *   The form.
  */
 function _workflow_transition_form_element_validate_time($element, FormStateInterface &$form_state, $form) {
-- 
GitLab