Skip to content
Snippets Groups Projects

Improved assertion in #lazy_builder: loop to assert params individually.

Open Cesar Morua requested to merge issue/drupal-3455746:3455746-a-lazybuilder-callbacks into 11.x
All threads resolved!
@@ -369,9 +369,11 @@ protected function doRender(array &$elements, RenderContext $context): string|Ma
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(is_array($elements['#lazy_builder'][1]), 'The #lazy_builder argument for callback must have an array as a value.');
assert(count($elements['#lazy_builder'][1]) === count(array_filter($elements['#lazy_builder'][1], function ($v) {
return is_null($v) || is_scalar($v);
})), "A #lazy_builder callback's context may only contain scalar values or NULL.");
// Utilize an outer assertion to ensure array_walk() is only called when assertions are enabled.
assert(array_walk($elements['#lazy_builder'][1], function ($arg, $key) {
assert(is_null($arg) || is_scalar($arg),
"A #lazy_builder callback's context may only contain scalar values or NULL. Problem with parameter at array key: " . $key);
}));
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',
Loading