From 85d08d1cfbea6353eed27f9b710daede597fdaf6 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sat, 16 Mar 2024 18:00:17 +0000
Subject: [PATCH] Issue #3083690 by gxleano, mparker17, jungle, joachim,
 Kris77: '"@key" is an invalid render array key' error in
 \Drupal\Core\Render\Element is misleading

(cherry picked from commit ee4eaba6ffc540549fe07d98872cf92c4bddc8e0)
---
 core/lib/Drupal/Core/Render/Element.php             | 8 +++++++-
 core/tests/Drupal/Tests/Core/Render/ElementTest.php | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/core/lib/Drupal/Core/Render/Element.php b/core/lib/Drupal/Core/Render/Element.php
index 88793bc21777..1fd24219c5ca 100644
--- a/core/lib/Drupal/Core/Render/Element.php
+++ b/core/lib/Drupal/Core/Render/Element.php
@@ -95,7 +95,13 @@ public static function children(array &$elements, $sort = FALSE) {
         // Only trigger an error if the value is not null.
         // @see https://www.drupal.org/node/1283892
         elseif (isset($value)) {
-          trigger_error(new FormattableMarkup('"@key" is an invalid render array key', ['@key' => $key]), E_USER_ERROR);
+          trigger_error(new FormattableMarkup(
+            '"@key" is an invalid render array key. Value should be an array but got a @type',
+            [
+              '@key' => $key,
+              '@type' => gettype($value),
+            ]
+          ), E_USER_ERROR);
         }
       }
       $i++;
diff --git a/core/tests/Drupal/Tests/Core/Render/ElementTest.php b/core/tests/Drupal/Tests/Core/Render/ElementTest.php
index 31f3a7b8a2c6..5795e8da7a1b 100644
--- a/core/tests/Drupal/Tests/Core/Render/ElementTest.php
+++ b/core/tests/Drupal/Tests/Core/Render/ElementTest.php
@@ -109,7 +109,7 @@ public function testInvalidChildren() {
       'foo' => 'bar',
     ];
     $this->expectError();
-    $this->expectErrorMessage('"foo" is an invalid render array key');
+    $this->expectErrorMessage('"foo" is an invalid render array key. Value should be an array but got a string');
     Element::children($element);
   }
 
-- 
GitLab