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

Command icon 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:

Comments

prudloff created an issue. See original summary.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

prudloff’s picture

Issue summary: View changes
prudloff’s picture

Issue summary: View changes
Status: Active » Needs review

I tried to find the best place to document this because:

  • Adding a closure in ElementInterface::getInfo() or hook_element_info_alter will always cause an error if you have cache enabled.
  • Adding a closure later (for example when altering the render array) could work (if the render array is never serialized) but is still risky.

Do we need to add a test that shows the problem or is updating the doc enough?

smustgrave’s picture

Status: Needs review » Needs work

Appears to have already been reviewed by @larowlan

prudloff’s picture

Status: Needs work » Needs review

I applied the suggestion.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Feedback from @larowlan I believe has been addressed here.

  • catch committed 9cdb82c7 on 11.5.x
    docs: #3551942 Exception when using a closure in #pre_render
    
    By:...

  • catch committed 3c739272 on 11.x
    docs: #3551942 Exception when using a closure in #pre_render
    
    By:...

  • catch committed d24526e2 on 12.0.x
    docs: #3551942 Exception when using a closure in #pre_render
    
    By:...

  • catch committed 28517aad on main
    docs: #3551942 Exception when using a closure in #pre_render
    
    By:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to main and cherry-picked back through to 11.5.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.