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:
(DrupalCoreEntityEntityStorageException)
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:xampphtdocsgosrcorelibDrupalCoreRenderRenderer.php:243
DrupalCoreRenderRenderer->doRender() at C:xampphtdocsgosrcorelibDrupalCoreRenderRenderer.php:200
DrupalCoreRenderRenderer->render() at C:xampphtdocsgosrmodulescontribserialsrcPluginFieldFieldFormatterSerialDefaultFormatter.php:33
DrupalserialPluginFieldFieldFormatterSerialDefaultFormatter->viewElements() at C:xampphtdocsgosrcorelibDrupalCoreFieldFormatterBase.php:89
DrupalCoreFieldFormatterBase->view() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityEntityViewDisplay.php:263
DrupalCoreEntityEntityEntityViewDisplay->buildMultiple() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityEntityViewDisplay.php:221
DrupalCoreEntityEntityEntityViewDisplay->build() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityViewBuilder.php:472
DrupalCoreEntityEntityViewBuilder->viewField() at C:xampphtdocsgosrcorelibDrupalCoreFieldFieldItemList.php:243
DrupalCoreFieldFieldItemList->view() at C:xampphtdocsgosrmodulescontribtokentoken.tokens.inc:1630
field_tokens() at n/a:n/a
call_user_func_array() at C:xampphtdocsgosrcorelibDrupalCoreExtensionModuleHandler.php:403
DrupalCoreExtensionModuleHandler->invokeAll() at C:xampphtdocsgosrcorelibDrupalCoreUtilityToken.php:304
DrupalCoreUtilityToken->generate() at C:xampphtdocsgosrmodulescontribtokentoken.tokens.inc:937
token_tokens() at n/a:n/a
call_user_func_array() at C:xampphtdocsgosrcorelibDrupalCoreExtensionModuleHandler.php:403
DrupalCoreExtensionModuleHandler->invokeAll() at C:xampphtdocsgosrcorelibDrupalCoreUtilityToken.php:304
DrupalCoreUtilityToken->generate() at C:xampphtdocsgosrcorelibDrupalCoreUtilityToken.php:196
DrupalCoreUtilityToken->replace() at C:xampphtdocsgosrmodulescontribauto_entitylabelsrcAutoEntityLabelManager.php:264
Drupalauto_entitylabelAutoEntityLabelManager->generateLabel() at C:xampphtdocsgosrmodulescontribauto_entitylabelsrcAutoEntityLabelManager.php:145
Drupalauto_entitylabelAutoEntityLabelManager->setLabel() at C:xampphtdocsgosrmodulescontribauto_entitylabelauto_entitylabel.module:170
auto_entitylabel_entity_presave() at n/a:n/a
call_user_func_array() at C:xampphtdocsgosrcorelibDrupalCoreExtensionModuleHandler.php:403
DrupalCoreExtensionModuleHandler->invokeAll() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityStorageBase.php:206
DrupalCoreEntityEntityStorageBase->invokeHook() at C:xampphtdocsgosrcorelibDrupalCoreEntityContentEntityStorageBase.php:843
DrupalCoreEntityContentEntityStorageBase->invokeHook() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityStorageBase.php:500
DrupalCoreEntityEntityStorageBase->doPreSave() at C:xampphtdocsgosrcorelibDrupalCoreEntityContentEntityStorageBase.php:700
DrupalCoreEntityContentEntityStorageBase->doPreSave() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityStorageBase.php:454
DrupalCoreEntityEntityStorageBase->save() at C:xampphtdocsgosrcorelibDrupalCoreEntitySqlSqlContentEntityStorage.php:837
DrupalCoreEntitySqlSqlContentEntityStorage->save() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityBase.php:395
DrupalCoreEntityEntityBase->save() at C:xampphtdocsgosrmodulescustomgosr_modscript_update_nb_nuitees.php:75
include() at C:xampphtdocsgosrvendordrushdrushsrcCommandscorePhpCommands.php:111
DrushCommandscorePhpCommands->script() at n/a:n/a
call_user_func_array() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcCommandProcessor.php:257
ConsolidationAnnotatedCommandCommandProcessor->runCommandCallback() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcCommandProcessor.php:212
ConsolidationAnnotatedCommandCommandProcessor->validateRunAndAlter() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcCommandProcessor.php:176
ConsolidationAnnotatedCommandCommandProcessor->process() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcAnnotatedCommand.php:302
ConsolidationAnnotatedCommandAnnotatedCommand->execute() at C:xampphtdocsgosrvendorsymfonyconsoleCommandCommand.php:255
SymfonyComponentConsoleCommandCommand->run() at C:xampphtdocsgosrvendorsymfonyconsoleApplication.php:1005
SymfonyComponentConsoleApplication->doRunCommand() at C:xampphtdocsgosrvendorsymfonyconsoleApplication.php:255
SymfonyComponentConsoleApplication->doRun() at C:xampphtdocsgosrvendorsymfonyconsoleApplication.php:148
SymfonyComponentConsoleApplication->run() at C:xampphtdocsgosrvendordrushdrushsrcRuntimeRuntime.php:118
DrushRuntimeRuntime->doRun() at C:xampphtdocsgosrvendordrushdrushsrcRuntimeRuntime.php:49
DrushRuntimeRuntime->run() at C:xampphtdocsgosrvendordrushdrushdrush.php:72
require() at C:xampphtdocsgosrvendordrushdrushdrush:4
Stack trace for EntityStorageException
Exception trace:
at C:xampphtdocsgosrcorelibDrupalCoreEntitySqlSqlContentEntityStorage.php:846
DrupalCoreEntitySqlSqlContentEntityStorage->save() at C:xampphtdocsgosrcorelibDrupalCoreEntityEntityBase.php:395
DrupalCoreEntityEntityBase->save() at C:xampphtdocsgosrmodulescustomgosr_modscript_update_nb_nuitees.php:75
include() at C:xampphtdocsgosrvendordrushdrushsrcCommandscorePhpCommands.php:111
DrushCommandscorePhpCommands->script() at n/a:n/a
call_user_func_array() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcCommandProcessor.php:257
ConsolidationAnnotatedCommandCommandProcessor->runCommandCallback() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcCommandProcessor.php:212
ConsolidationAnnotatedCommandCommandProcessor->validateRunAndAlter() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcCommandProcessor.php:176
ConsolidationAnnotatedCommandCommandProcessor->process() at C:xampphtdocsgosrvendorconsolidationannotated-commandsrcAnnotatedCommand.php:302
ConsolidationAnnotatedCommandAnnotatedCommand->execute() at C:xampphtdocsgosrvendorsymfonyconsoleCommandCommand.php:255
SymfonyComponentConsoleCommandCommand->run() at C:xampphtdocsgosrvendorsymfonyconsoleApplication.php:1005
SymfonyComponentConsoleApplication->doRunCommand() at C:xampphtdocsgosrvendorsymfonyconsoleApplication.php:255
SymfonyComponentConsoleApplication->doRun() at C:xampphtdocsgosrvendorsymfonyconsoleApplication.php:148
SymfonyComponentConsoleApplication->run() at C:xampphtdocsgosrvendordrushdrushsrcRuntimeRuntime.php:118
DrushRuntimeRuntime->doRun() at C:xampphtdocsgosrvendordrushdrushsrcRuntimeRuntime.php:49
DrushRuntimeRuntime->run() at C:xampphtdocsgosrvendordrushdrushdrush.php:72
require() at C:xampphtdocsgosrvendordrushdrushdrush:4