0

I wrote a function that updates fields of an "Intervention" content type and saves it, then saves the "Request" node that contains it, and then saves the "Dossier" node that contains that request.

Running that code in a Devel PHP form work fine.

Now I'm trying to convert that function into a drush script that I could schedule to run everyday. But running that script gives me a couple of errors that I don't understand.

In Renderer.php line 243:

[LogicException] Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPla in()/renderRoot() or #lazy_builder/#pre_render instead.

In SqlContentEntityStorage.php line 846:

[Drupal\Core\Entity\EntityStorageException] Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPla in()/renderRoot() or #lazy_builder/#pre_render instead.

Looking at the stack trace, it looks like a save() call on an entity is triggering these errors ... does anybody know what needs to be done to correct them?

Thank you!

Function code ... comments are in french, sorry

$query = \Drupal::entityQuery('node')
->condition('type', 'intervention')
->condition('field_type_intervention', '318')
->sort('changed', 'ASC');

$nids = $query->execute();
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$nodes = $node_storage->loadMultiple($nids);
$i = 0;

// pour tous ces hébergements
foreach ($nodes as $n) {
  echo $n->title->value . "<br>";

  $dateDebutVal = $n->field_date_debut_intervention_1->value;
  $dateFinVal = $n->field_date_fin_intervention_1->value;

  $nbChambres = $n->field_nombre_chambres->value;
  $tarifNuitee = $n->field_tarif_nuitee->value;

  echo  'dateDebut='.$dateDebutVal. '<br>';
  echo  'dateFin='.$dateFinVal. '<br>';
  $dateDebut = strtotime($dateDebutVal);

  if ($dateFinVal != "") {
    $dateFin = strtotime($dateFinVal);
  }
  else {
    $dateFin = time();
  }

  $datediff = $dateFin - $dateDebut;
  $nbJours = floor($datediff / (60 * 60 * 24));

  if ($nbJours == 0) {
    $nbJours++;
  }

  echo 'nbJours = '.$nbJours. '<br>';
  $n->set('field_nombre_nuitee', $nbJours);

  // si le nombre de chambre et le tarif nuitée sont settés, calculer le montant estimé
  if ($nbChambres != "" && $tarifNuitee != "") {
    echo 'estimation = '.number_format($tarifNuitee * $nbChambres * $nbJours, 2, '.', ''). '<br>';
    $n->set('field_cout_estime', number_format($tarifNuitee * $nbChambres * $nbJours, 2, '.', ''));
  }

  // sauvegarder l'hébergement
  $n->save();

  // aller chercher la requête de cet hébergement et la sauvegarder
  $queryRequete = \Drupal::entityQuery('node')
    ->condition('type', 'requete')
    ->condition('field_intervention_service', $n->id())
    ->sort('changed', 'ASC');

  $nidsRequete = $queryRequete->execute();
  $requetes = $node_storage->loadMultiple($nidsRequete);

  foreach ($requetes as $r) {
    echo $r->title->value . '<br>';
    $r->save();

    $queryDossier = \Drupal::entityQuery('node')
      ->condition('type', 'dossier')
      ->condition('field_requete_menage', $r->id())
      ->sort('changed', 'ASC');

    $nidsDossier = $queryDossier->execute();
    $dossiers = $node_storage->loadMultiple($nidsDossier);

    foreach ($dossiers as $d) {
      echo $d->title->value . '<br>';
      $d->save();
    }
  }

  // aller chercher le dossier lié à cette requête et le sauvegarder
  $i++;
}

echo 'i = '.$i. '<br>';

Stack trace for LogicException

Exception trace: at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Render\Renderer.php:243 Drupal\Core\Render\Renderer->doRender() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Render\Renderer.php:200 Drupal\Core\Render\Renderer->render() at C:\xampp\htdocs\gosr\modules\contrib\serial\src\Plugin\Field\FieldFormatter\SerialDefaultFormatter.php:33 Drupal\serial\Plugin\Field\FieldFormatter\SerialDefaultFormatter->viewElements() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Field\FormatterBase.php:89 Drupal\Core\Field\FormatterBase->view() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\Entity\EntityViewDisplay.php:263 Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\Entity\EntityViewDisplay.php:221 Drupal\Core\Entity\Entity\EntityViewDisplay->build() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\EntityViewBuilder.php:472 Drupal\Core\Entity\EntityViewBuilder->viewField() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Field\FieldItemList.php:243 Drupal\Core\Field\FieldItemList->view() at C:\xampp\htdocs\gosr\modules\contrib\token\token.tokens.inc:1630 field_tokens() at n/a:n/a call_user_func_array() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Extension\ModuleHandler.php:403 Drupal\Core\Extension\ModuleHandler->invokeAll() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Utility\Token.php:304 Drupal\Core\Utility\Token->generate() at C:\xampp\htdocs\gosr\modules\contrib\token\token.tokens.inc:937 token_tokens() at n/a:n/a call_user_func_array() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Extension\ModuleHandler.php:403 Drupal\Core\Extension\ModuleHandler->invokeAll() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Utility\Token.php:304 Drupal\Core\Utility\Token->generate() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Utility\Token.php:196 Drupal\Core\Utility\Token->replace() at C:\xampp\htdocs\gosr\modules\contrib\auto_entitylabel\src\AutoEntityLabelManager.php:264 Drupal\auto_entitylabel\AutoEntityLabelManager->generateLabel() at C:\xampp\htdocs\gosr\modules\contrib\auto_entitylabel\src\AutoEntityLabelManager.php:145 Drupal\auto_entitylabel\AutoEntityLabelManager->setLabel() at C:\xampp\htdocs\gosr\modules\contrib\auto_entitylabel\auto_entitylabel.module:170 auto_entitylabel_entity_presave() at n/a:n/a call_user_func_array() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Extension\ModuleHandler.php:403 Drupal\Core\Extension\ModuleHandler->invokeAll() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\EntityStorageBase.php:206 Drupal\Core\Entity\EntityStorageBase->invokeHook() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\ContentEntityStorageBase.php:843 Drupal\Core\Entity\ContentEntityStorageBase->invokeHook() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\EntityStorageBase.php:500 Drupal\Core\Entity\EntityStorageBase->doPreSave() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\ContentEntityStorageBase.php:700 Drupal\Core\Entity\ContentEntityStorageBase->doPreSave() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\EntityStorageBase.php:454 Drupal\Core\Entity\EntityStorageBase->save() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php:837 Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\EntityBase.php:395 Drupal\Core\Entity\EntityBase->save() at C:\xampp\htdocs\gosr\modules\custom\gosr_mod\script_update_nb_nuitees.php:75 include() at C:\xampp\htdocs\gosr\vendor\drush\drush\src\Commands\core\PhpCommands.php:111 Drush\Commands\core\PhpCommands->script() at n/a:n/a call_user_func_array() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\CommandProcessor.php:257 Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\CommandProcessor.php:212 Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\CommandProcessor.php:176 Consolidation\AnnotatedCommand\CommandProcessor->process() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\AnnotatedCommand.php:302 Consolidation\AnnotatedCommand\AnnotatedCommand->execute() at C:\xampp\htdocs\gosr\vendor\symfony\console\Command\Command.php:255 Symfony\Component\Console\Command\Command->run() at C:\xampp\htdocs\gosr\vendor\symfony\console\Application.php:1005 Symfony\Component\Console\Application->doRunCommand() at C:\xampp\htdocs\gosr\vendor\symfony\console\Application.php:255 Symfony\Component\Console\Application->doRun() at C:\xampp\htdocs\gosr\vendor\symfony\console\Application.php:148 Symfony\Component\Console\Application->run() at C:\xampp\htdocs\gosr\vendor\drush\drush\src\Runtime\Runtime.php:118 Drush\Runtime\Runtime->doRun() at C:\xampp\htdocs\gosr\vendor\drush\drush\src\Runtime\Runtime.php:49 Drush\Runtime\Runtime->run() at C:\xampp\htdocs\gosr\vendor\drush\drush\drush.php:72 require() at C:\xampp\htdocs\gosr\vendor\drush\drush\drush:4

Stack trace for EntityStorageException

Exception trace: at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php:846 Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() at C:\xampp\htdocs\gosr\core\lib\Drupal\Core\Entity\EntityBase.php:395 Drupal\Core\Entity\EntityBase->save() at C:\xampp\htdocs\gosr\modules\custom\gosr_mod\script_update_nb_nuitees.php:75 include() at C:\xampp\htdocs\gosr\vendor\drush\drush\src\Commands\core\PhpCommands.php:111 Drush\Commands\core\PhpCommands->script() at n/a:n/a call_user_func_array() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\CommandProcessor.php:257 Consolidation\AnnotatedCommand\CommandProcessor->runCommandCallback() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\CommandProcessor.php:212 Consolidation\AnnotatedCommand\CommandProcessor->validateRunAndAlter() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\CommandProcessor.php:176 Consolidation\AnnotatedCommand\CommandProcessor->process() at C:\xampp\htdocs\gosr\vendor\consolidation\annotated-command\src\AnnotatedCommand.php:302 Consolidation\AnnotatedCommand\AnnotatedCommand->execute() at C:\xampp\htdocs\gosr\vendor\symfony\console\Command\Command.php:255 Symfony\Component\Console\Command\Command->run() at C:\xampp\htdocs\gosr\vendor\symfony\console\Application.php:1005 Symfony\Component\Console\Application->doRunCommand() at C:\xampp\htdocs\gosr\vendor\symfony\console\Application.php:255 Symfony\Component\Console\Application->doRun() at C:\xampp\htdocs\gosr\vendor\symfony\console\Application.php:148 Symfony\Component\Console\Application->run() at C:\xampp\htdocs\gosr\vendor\drush\drush\src\Runtime\Runtime.php:118 Drush\Runtime\Runtime->doRun() at C:\xampp\htdocs\gosr\vendor\drush\drush\src\Runtime\Runtime.php:49 Drush\Runtime\Runtime->run() at C:\xampp\htdocs\gosr\vendor\drush\drush\drush.php:72 require() at C:\xampp\htdocs\gosr\vendor\drush\drush\drush:4

2
  • 1
    Looks like you have an old version of the Serial Field module - the render call is fixed in the current alpha release – Clive Mar 1 at 20:40
  • Yep! That solved it. Thank you very much! – agodin Mar 1 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.