$node->save() fails when run in post_update

I have a simple hook_post_update function which fails with the following error:

[notice] Update started: ssc_custom_post_update_9001

[error] 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 DrupalCoreRenderRenderer->doRender() (line 244 of E:wwwmysschtmlcorelibDrupalCoreRenderRenderer.php). [error] Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_render instead. [error] Update failed: ssc_custom_post_update_9001

In ProcessBase.php line 171:

Unable to decode output into JSON: Syntax error

[ERROR] [node] [2022-03-12T04:58:35] LogicException: Render context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use renderPlain()/renderRoot() or #lazy_builder/#pre_re nder instead. in DrupalCoreRenderRenderer->doRender() (line 244 of E:wwwmysschtmlcorelibDrupalC oreRenderRenderer.php). | uid: 0 | request-uri: http://default/ | refer: | ip: 127.0.0.1 | link:
{ "0": { "ssc_custom": { "9001": { "#abort": { "success": false, "query": "DrupalCoreEntityEntityStorageException: Ren der context is empty, because render() was called outside of a renderRoot() or renderPlain() call. Use re nderPlain()/renderRoot() or #lazy_builder/#pre_render instead. in DrupalCoreEnt
itySqlSqlContentEntityStorage->save() (line 810 of E:wwwmysschtmlcorelibDrupalCoreEntitySqlSqlContentEntityStorage.php)." } } }, "#abort": [ "ssc_custom_post_update_9001" ] }, "drush_batch_process_finished": true }

I have stripped the code down to the bare minimum:

use DrupalCoreEntityEntityStorageInterface; use DrupalCoreEntityEntityTypeManagerInterface; use DrupalnodeNodeInterface;  function ssc_custom_post_update_9001(&$sandbox) {   /** @var DrupalnodeNodeStorageInterface $node_storage */   $node_storage = Drupal::entityTypeManager()->getStorage('node');    // How many entities to processed per batch.   $limit = 5;    $node_ids = $node_storage->getQuery()     ->accessCheck(FALSE)     ->condition('type', 'article')     ->range(0, $limit)     ->execute();   // Load entities.   $nodes = $node_storage->loadMultiple($node_ids);    /** @var DrupalnodeNodeInterface $node */   foreach ($nodes as $node) {     $node->setNewRevision();     $node->save();   }    $sandbox['#finished'] = 1;  } 

If I run the same code directly (not from drush updb) it runs fine. Running from the admin menu "Run updates"; it also fails (so it is not a drush issue).

Commenting out the $node->save() and the error does not occur.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

$node->save() fails when run in post_update

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.