From e9a53cb2b38fe42cdd0f68876f19dc922b13ed0e Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 3 May 2021 23:56:39 +0100 Subject: [PATCH] Issue #3143096 by jedihe, jyotimishra123, shetpooja04, alexpott, mradcliffe: [DX]: throw an exception if #lazy_builder callback does not return a (renderable) array --- core/lib/Drupal/Core/Render/Renderer.php | 17 +++++++ .../Core/Render/RendererPlaceholdersTest.php | 48 +++++++++++++++++++ .../Tests/Core/Render/RendererTestBase.php | 12 ++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 02620cbf1113..37d99bd64223 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -353,6 +353,23 @@ protected function doRender(&$elements, $is_root_call = FALSE) { // Build the element if it is still empty. if (isset($elements['#lazy_builder'])) { $new_elements = $this->doCallback('#lazy_builder', $elements['#lazy_builder'][0], $elements['#lazy_builder'][1]); + // Throw an exception if #lazy_builder callback does not return an array; + // provide helpful details for troubleshooting. + if (!is_array($new_elements)) { + $callable = $elements['#lazy_builder'][0]; + $callable_name = '[unknown]'; + if ($callable instanceof \Closure) { + $callable_name = '[closure]'; + } + elseif (is_array($callable)) { + $callable_name = implode('::', $callable); + } + elseif (is_string($callable)) { + $callable_name = $callable; + } + $wrong_type = gettype($new_elements); + throw new \LogicException("#lazy_builder callbacks must return a valid renderable array, got $wrong_type from $callable_name"); + } // Retain the original cacheability metadata, plus cache keys. CacheableMetadata::createFromRenderArray($elements) ->merge(CacheableMetadata::createFromRenderArray($new_elements)) diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php index 7478840c5d64..6bb477450659 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php @@ -962,6 +962,54 @@ public function testCreatePlaceholderPropertyWithoutLazyBuilder() { $this->renderer->renderRoot($element); } + /** + * Tests lazy builders (string callable) that do not return a renderable. + * + * @covers ::render + * @covers ::doRender + */ + public function testNonArrayReturnFromLazyBuilderStringCallable() { + $element = []; + $element['#lazy_builder'] = ['Drupal\Tests\Core\Render\PlaceholdersTest::callbackNonArrayReturn', []]; + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('#lazy_builder callbacks must return a valid renderable array, got boolean from Drupal\Tests\Core\Render\PlaceholdersTest::callbackNonArrayReturn'); + $this->renderer->renderRoot($element); + } + + /** + * Tests lazy builders (array callable) that do not return a renderable. + * + * @covers ::render + * @covers ::doRender + */ + public function testNonArrayReturnFromLazyBuilderArrayCallable() { + $element = []; + $element['#lazy_builder'] = [['Drupal\Tests\Core\Render\PlaceholdersTest', 'callbackNonArrayReturn'], []]; + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('#lazy_builder callbacks must return a valid renderable array, got boolean from Drupal\Tests\Core\Render\PlaceholdersTest::callbackNonArrayReturn'); + $this->renderer->renderRoot($element); + } + + /** + * Tests lazy builders (closure) that do not return a renderable. + * + * @covers ::render + * @covers ::doRender + */ + public function testNonArrayReturnFromLazyBuilderClosure() { + $element = []; + $closure = function () { + return NULL; + }; + $element['#lazy_builder'] = [$closure, []]; + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('#lazy_builder callbacks must return a valid renderable array, got NULL from [closure]'); + $this->renderer->renderRoot($element); + } + /** * Create an element with a child and subchild. Each element has the same * #lazy_builder callback, but with different contexts. They don't modify diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php index 651219463181..49c6b361b56f 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTestBase.php @@ -317,11 +317,21 @@ public static function callbackTagCurrentTemperature($animal) { return $build; } + /** + * Returns invalid renderable to #lazy_builder callback. + * + * @return bool + * TRUE. + */ + public static function callbackNonArrayReturn() { + return TRUE; + } + /** * {@inheritdoc} */ public static function trustedCallbacks() { - return ['callbackTagCurrentTemperature', 'callbackPerUser', 'callback']; + return ['callbackTagCurrentTemperature', 'callbackPerUser', 'callback', 'callbackNonArrayReturn']; } } -- GitLab