Change record status: 
Project: 
Introduced in branch: 
9.3.x
Introduced in version: 
9.3.0
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

matthieuscarset’s picture

For the record, here's the documentation with the most accurate/up-to-date information about D8+ render pipeline.

m4olivei’s picture

There is an edge case to be aware of in straight converting from $output = render($build); to $output = \Drupal::service('renderer')->render($build);. The old render method has a safe-guard for inappropriate use of render, eg. just passing a simple string to render as in:

$thing = 'foo bar';
$output = render($thing);

Converting that to:

$thing = 'foo bar';
$output = \Drupal::service('renderer')->render($thing);

Will result in errors.

There are a couple ways to fix this. First, realize that your use of render was 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 like

Before:

$thing = 'foo bar';
$output = render($thing);

After:

$thing = 'foo bar';
$output = \Drupal::service('renderer')->render(['#markup' => $thing]);

You could also guard against the situation in your code, like render was doing for you, eg.

$thing = 'foo bar';
if (is_array($thing)) {
  $output = \Drupal::service('renderer')->render($thing);
}
else {
  $output = $thing;
}

Of course this is a contrived example, but there are more obscure real world examples where this is an issue needing to be addressed.

no sssweat’s picture

With

$output = \Drupal::service('renderer')->render(['#markup' => $thing]);

I got a

Error: Drupal\Core\Render\Renderer::render(): Argument #1 ($elements) cannot be passed by reference in mytheme_preprocess_page()

Had to

  $render_array = [
    '#markup' => $thing
  ];
  $footer_contact = \Drupal::service('renderer')->render($render_array);