Skip to main content
add code example for rendering a view in isolation
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You can reproduce the error with Drush:

# drush php

>>> $build = ['#markup' => 'TEST'];
=> [
     "#markup" => "TEST",
   ]

>>> $rendered = \Drupal::service('renderer')->render($build);
LogicException with message 'Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead.'

>>> $rendered = \Drupal::service('renderer')->renderPlain($build);
=> Drupal\Core\Render\Markup {#4668
     markup: "TEST",
   }

So as the error message says modules which hook into the node save process should use renderPlain() instead. They shouldn't assume nodes are always saved within a render context of a themed Drupal page.

Code example for rendering a view in isolation:

$build = $view->buildRenderable();
$rendered = \Drupal::service('renderer')->renderPlain($build);

You can reproduce the error with Drush:

# drush php

>>> $build = ['#markup' => 'TEST'];
=> [
     "#markup" => "TEST",
   ]

>>> $rendered = \Drupal::service('renderer')->render($build);
LogicException with message 'Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead.'

>>> $rendered = \Drupal::service('renderer')->renderPlain($build);
=> Drupal\Core\Render\Markup {#4668
     markup: "TEST",
   }

So as the error message says modules which hook into the node save process should use renderPlain() instead. They shouldn't assume nodes are always saved within a render context of a themed Drupal page.

You can reproduce the error with Drush:

# drush php

>>> $build = ['#markup' => 'TEST'];
=> [
     "#markup" => "TEST",
   ]

>>> $rendered = \Drupal::service('renderer')->render($build);
LogicException with message 'Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead.'

>>> $rendered = \Drupal::service('renderer')->renderPlain($build);
=> Drupal\Core\Render\Markup {#4668
     markup: "TEST",
   }

So as the error message says modules which hook into the node save process should use renderPlain() instead. They shouldn't assume nodes are always saved within a render context of a themed Drupal page.

Code example for rendering a view in isolation:

$build = $view->buildRenderable();
$rendered = \Drupal::service('renderer')->renderPlain($build);
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You can reproduce the error with Drush:

# drush php

>>> $build = ['#markup' => 'TEST'];
=> [
     "#markup" => "TEST",
   ]

>>> $rendered = \Drupal::service('renderer')->render($build);
LogicException with message 'Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead.'

>>> $rendered = \Drupal::service('renderer')->renderPlain($build);
=> Drupal\Core\Render\Markup {#4668
     markup: "TEST",
   }

So as the error message says modules which hook into the node save process should use renderPlain() instead. They shouldn't assume nodes are always saved within a render context of a themed Drupal page.