Problem/Motivation
This CR says render callbacks can be closures: https://www.drupal.org/node/2966725
It does say that anonymous functions are not recommended but this is not documented in the RenderElementBase docblock.
The docblock for RenderElementBase says that #pre_render is an "Array of callables" and does not mention that closures can cause errors if serialized.
But using a closure in #pre_render can trigger this exception when ElementInfoManager::buildInfo() tries to serialize the array:
Serialization of 'Closure' is not allowed
However depending on where the #pre_render is added, it sometimes works. It only causes an error if the render array is serialized.
Steps to reproduce
/**
* Implements hook_element_info_alter().
*/
function test_element_info_alter(array &$info): void {
$info['link']['#pre_render'][] = array_filter(...);
}
Proposed resolution
I'm not sure there is an easy way to support this. There are some libraries to serialize closures (https://github.com/laravel/serializable-closure for example) but it does not feel right to do this.
But maybe the doc could be improved.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3551942
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3551942-exception-when-using
changes, plain diff MR !14805
Comments
Comment #4
prudloff commentedComment #5
prudloff commentedI tried to find the best place to document this because:
Do we need to add a test that shows the problem or is updating the doc enough?
Comment #6
smustgrave commentedAppears to have already been reviewed by @larowlan
Comment #7
prudloff commentedI applied the suggestion.
Comment #8
smustgrave commentedFeedback from @larowlan I believe has been addressed here.
Comment #13
catchCommitted/pushed to main and cherry-picked back through to 11.5.x, thanks!