In a custom module (sel), I have this code:
function sel_cron()
{
$accountSwitcher = Drupal::service('account_switcher');
$account = \Drupal\user\Entity\User::load(1);
$accountSwitcher->switchTo($account);
$query = \Drupal::entityQuery('service_category');
$dLastExtractionDate = strftime("%Y-%m-%d", strtotime("- 1 week", strtotime(\Drupal::state()->get('sel.NextEmailForNewServices'))));
$view = views_embed_view('sel_services', 'embed_2', $dLastExtractionDate);
$view = \Drupal::service('renderer')->render($view); <--------------------- problem here
$accountSwitcher->switchBack();
}
The expected result of the view is a table but when the result is empty (because of the value of the contextual filter), an unfiltered text ("Nothing to display today. Too bad!") should be displayed instead.
But in this case, I get this error logged:
LogicException: Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead. in Drupal\Core\Render\Renderer->doRender() (line 243 of /Users/efalzon/Sites/JdP8/web/core/lib/Drupal/Core/Render/Renderer.php).
How to fix that?
\Drupal::service('plugin.manager.mail')->mail(some params);
renderPlain
, notrender
. See drupal.stackexchange.com/questions/292171/…