diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 2ea2ca113c0b195864f2c4525a2854e44eb1b6f7..9deb15d9f41948ebab3fd91dab6471d903dc63e6 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -304,23 +304,12 @@ protected function doRender(&$elements, $is_root_call = FALSE) { // First validate the usage of #lazy_builder; both of the next if-statements // use it if available. if (isset($elements['#lazy_builder'])) { - // @todo Convert to assertions once https://www.drupal.org/node/2408013 - // lands. - if (!is_array($elements['#lazy_builder'])) { - throw new \DomainException('The #lazy_builder property must have an array as a value.'); - } - if (count($elements['#lazy_builder']) !== 2) { - throw new \DomainException('The #lazy_builder property must have an array as a value, containing two values: the callback, and the arguments for the callback.'); - } - if (count($elements['#lazy_builder'][1]) !== count(array_filter($elements['#lazy_builder'][1], function ($v) { + assert(is_array($elements['#lazy_builder']), 'The #lazy_builder property must have an array as a value.'); + assert(count($elements['#lazy_builder']) === 2, 'The #lazy_builder property must have an array as a value, containing two values: the callback, and the arguments for the callback.'); + assert(count($elements['#lazy_builder'][1]) === count(array_filter($elements['#lazy_builder'][1], function ($v) { return is_null($v) || is_scalar($v); - }))) { - throw new \DomainException("A #lazy_builder callback's context may only contain scalar values or NULL."); - } - $children = Element::children($elements); - if ($children) { - throw new \DomainException(sprintf('When a #lazy_builder callback is specified, no children can exist; all children must be generated by the #lazy_builder callback. You specified the following children: %s.', implode(', ', $children))); - } + })), "A #lazy_builder callback's context may only contain scalar values or NULL."); + assert(!Element::children($elements), sprintf('When a #lazy_builder callback is specified, no children can exist; all children must be generated by the #lazy_builder callback. You specified the following children: %s.', implode(', ', Element::children($elements)))); $supported_keys = [ '#lazy_builder', '#cache', @@ -331,10 +320,7 @@ protected function doRender(&$elements, $is_root_call = FALSE) { '#weight', '#printed', ]; - $unsupported_keys = array_diff(array_keys($elements), $supported_keys); - if (count($unsupported_keys)) { - throw new \DomainException(sprintf('When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: %s.', implode(', ', $unsupported_keys))); - } + assert(empty(array_diff(array_keys($elements), $supported_keys)), sprintf('When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: %s.', implode(', ', array_diff(array_keys($elements), $supported_keys)))); } // Determine whether to do auto-placeholdering. if ($this->placeholderGenerator->canCreatePlaceholder($elements) && $this->placeholderGenerator->shouldAutomaticallyPlaceholder($elements)) { diff --git a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php index 1d41764d35b4fa8b85deb12887e501edadb76e74..d13b416140081a71b0fce5ddfa4ec99c6b8bc1a7 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererPlaceholdersTest.php @@ -853,7 +853,7 @@ public function testInvalidLazyBuilder() { $element = []; $element['#lazy_builder'] = '\Drupal\Tests\Core\Render\PlaceholdersTest::callback'; - $this->expectException(\DomainException::class); + $this->expectException(\AssertionError::class); $this->expectExceptionMessage('The #lazy_builder property must have an array as a value.'); $this->renderer->renderRoot($element); } @@ -866,7 +866,7 @@ public function testInvalidLazyBuilderArguments() { $element = []; $element['#lazy_builder'] = ['\Drupal\Tests\Core\Render\PlaceholdersTest::callback', 'arg1', 'arg2']; - $this->expectException(\DomainException::class); + $this->expectException(\AssertionError::class); $this->expectExceptionMessage('The #lazy_builder property must have an array as a value, containing two values: the callback, and the arguments for the callback.'); $this->renderer->renderRoot($element); } @@ -914,7 +914,7 @@ public function testNonScalarLazybuilderCallbackContext() { ], ]; - $this->expectException(\DomainException::class); + $this->expectException(\AssertionError::class); $this->expectExceptionMessage("A #lazy_builder callback's context may only contain scalar values or NULL."); $this->renderer->renderRoot($element); } @@ -929,7 +929,7 @@ public function testChildrenPlusBuilder() { $element['child_a']['#markup'] = 'Oh hai!'; $element['child_b']['#markup'] = 'kthxbai'; - $this->expectException(\DomainException::class); + $this->expectException(\AssertionError::class); $this->expectExceptionMessage('When a #lazy_builder callback is specified, no children can exist; all children must be generated by the #lazy_builder callback. You specified the following children: child_a, child_b.'); $this->renderer->renderRoot($element); } @@ -944,7 +944,7 @@ public function testPropertiesPlusBuilder() { $element['#llama'] = '#awesome'; $element['#piglet'] = '#cute'; - $this->expectException(\DomainException::class); + $this->expectException(\AssertionError::class); $this->expectExceptionMessage('When a #lazy_builder callback is specified, no properties can exist; all properties must be generated by the #lazy_builder callback. You specified the following properties: #llama, #piglet.'); $this->renderer->renderRoot($element); }