By oleksiy on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
9.3.x
Introduced in version:
9.3.0
Issue links:
Description:
The render() function in common.inc is deprecated. Use the render() method on the Renderer service instead.
Before:
$output = render($build);
After:
$output = \Drupal::service('renderer')->render($build);
Impacts:
Module developers
Comments
Documentation
For the record, here's the documentation with the most accurate/up-to-date information about D8+ render pipeline.
There is an edge case to be
There is an edge case to be aware of in straight converting from
$output = render($build);to$output = \Drupal::service('renderer')->render($build);. The oldrendermethod has a safe-guard for inappropriate use ofrender, eg. just passing a simple string to render as in:Converting that to:
Will result in errors.
There are a couple ways to fix this. First, realize that your use of
renderwas incorrect. The best way to fix would be to ensure you always have a render array being passed to\Drupal::service('renderer')->render, eg. your conversion would look more likeBefore:
After:
You could also guard against the situation in your code, like
renderwas doing for you, eg.Of course this is a contrived example, but there are more obscure real world examples where this is an issue needing to be addressed.
With
With
$output = \Drupal::service('renderer')->render(['#markup' => $thing]);I got a
Had to