Cannot resave nodes with entity->save(), no particular error

I’m trying to write a drush command to resave all the nodes on my site.

I searched for a module and found the Resave All Nodes module, but its drush command isn’t ready yet. So I decided to try to write it myself.

However, I can’t get my nodes to re-save with $entity->save(), and I don’t understand why.

<?php  namespace Drupalresave_all_nodesCommands;  use DrupalCoreEntityEntityTypeManagerInterface; use DrupalnodeEntityNode; use DrushCommandsDrushCommands;  /**  * A Drush command class for Resave All Nodes module conversions.  */ class ResaveAllNodesCommands extends DrushCommands {    /**    * The entity type manager.    *    * @var DrupalCoreEntityEntityTypeManagerInterface    */   private $entityTypeManager;     /**    * Constructs a ResaveAllNodesCommands object.    *    * @param DrupalCoreEntityEntityTypeManagerInterface $entity_type_manager    *   The entity type manager.    */   public function __construct(EntityTypeManagerInterface $entity_type_manager)   {     $this->entityTypeManager = $entity_type_manager;      parent::__construct();   }    /**    * Resave all nodes.    *    * @command resave-all-nodes:resave    *    * @usage drush resave-all-nodes:resave    *   Resave all nodes on the site.    *    * @aliases ran    */   public function resaveAllNodes()   {     $my_node = Node::load(1);     $my_node->save();     Drupal::logger('resave_all_nodes')->notice("node 1 saved!");   } } 

When I run the command, the logs show the logger notices:

  • I see "node 1 saved!" in the logs
  • I have another log message in hook_entity_presave(), which also appears

But, when I go to /admin/content, the "date updated" for node 1 has not changed. Also, the content of the node is not resaved.

If I go to node/1/edit and re-save manually, the node is re-saved as I would expect (the updated date is updated, and the field values are updated).

So, why does node->save() silently fail (the update date and field values remain the same), when saving manually works?

I have a few custom modules and I disabled them and rebuilt the cache, but the problem remains.

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

Cannot resave nodes with entity->save(), no particular error

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.