From e0bda88b3a36a32a21fc36587254b12e458825ad Mon Sep 17 00:00:00 2001
From: John Voskuilen <john.voskuilen@sapito.nl>
Date: Fri, 18 Apr 2025 08:20:31 +0200
Subject: [PATCH] Issue #3519831: Incorrect
 workflow_state_formatter['#label_display']

---
 src/Element/WorkflowTransitionElement.php |  2 +-
 workflow.field.inc                        | 12 +++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/Element/WorkflowTransitionElement.php b/src/Element/WorkflowTransitionElement.php
index 5195b8ea..254a0962 100644
--- a/src/Element/WorkflowTransitionElement.php
+++ b/src/Element/WorkflowTransitionElement.php
@@ -166,7 +166,7 @@ class WorkflowTransitionElement extends FormElement {
     if ($transition->isScheduled() || $transition->isExecuted() || !$show_widget) {
       $from_sid = $element[$attribute_name][$attribute_key]['#default_value'][0];
       $widget = workflow_state_formatter($entity, $field_name, $from_sid);
-      // @todo $widget['#label_display'] = 'before';
+      $widget['#label_display'] = 'before'; // 'above', 'hidden'.
       $widget['#access'] = TRUE;
     }
     $element[$attribute_name] = $widget;
diff --git a/workflow.field.inc b/workflow.field.inc
index 43e5df4d..1b2e66a2 100644
--- a/workflow.field.inc
+++ b/workflow.field.inc
@@ -52,14 +52,14 @@ function workflow_field_widget_info_alter(&$info) {
  * @return array
  *   Form element, resembling the formatter of List module.
  *   If state 0 is given, return an empty form element.
+ *   It may be needed to add the following after calling this function:
+ *   $widget['#label_display'] = 'before'; // 'above', 'hidden'.
  */
-function workflow_state_formatter(EntityInterface $entity, $field_name, $current_sid = '') {
-  $element = [];
 
-  if (!$current_sid) {
-    $current_sid = workflow_node_current_state($entity, $field_name);
-  }
+function workflow_state_formatter(EntityInterface $entity, $field_name, $current_sid = NULL) {
+  $element = [];
 
+  $current_sid ??= workflow_node_current_state($entity, $field_name);
   // Clone the entity and restore old value, in case you want to show an
   // executed transition.
   if ($entity->{$field_name}->value != $current_sid) {
@@ -73,6 +73,8 @@ function workflow_state_formatter(EntityInterface $entity, $field_name, $current
   // $list_display = $instance['display']['default']; .
   $list_display['type'] = 'list_default';
   $element = $entity->{$field_name}->view($list_display);
+  // Remove label_display to let caller determine it.
+  unset($element['#label_display']);
 
   // If user creates a node, and only 1 option is available, the formatter
   // is shown with key, not value, because creation state does not count.
-- 
GitLab