Cron with view got error: LogicException : Render context is empty, because render() was called outside of a renderRoot() or renderInIsolation() call

Created on 10 September 2024, 18 days ago
Updated 19 September 2024, 8 days ago

Problem/Motivation

I call the view in the cron hook and use advancedRender to get the value on the field (with rewrite).
it works fine with the browser.
but when I call the cron with the command line I get an error

LogicException : Render context is empty, because render() was called outside of a renderRoot() or renderInIsolation() call. Use renderInIsolation()/renderRoot() or #lazy_builder/#pre_render instead. dans Drupal\Core\Render\Renderer->doRender() (ligne 303 de /var/www/drupal10/web/core/lib/Drupal/Core/Render/Renderer.php)
it comes from $this->getRenderer()->render($value) in public function advancedRender(ResultRow $values) 

Steps to reproduce


/**
 * Implements hook_cron().
 */
function custom_cron() {
// Get the view.
    $view = Views::getView('relance');
    // Execute the view.
    $view->execute($display_id);
    $view->preview();
    foreach ($view->result as $rid => $row) {
      $message = $view->field['field_custom_message']->advancedRender($row)->__toString();
    }
  }

Proposed resolution

in core/modules/views/src/Plugin/views/field/FieldPluginBase.php
in line 1234, 1216, 1204

replace 
 $this->getRenderer()->render($value);
by
$this->getRenderer()->renderRoot($value); or
$this->getRenderer()->renderInIsolation($value);
πŸ’¬ Support request
Status

Active

Version

10.3 ✨

Component
ViewsΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡«πŸ‡·France lazzyvn paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024