Drupal 10: Save A Node Without Updating The Changed Time

To save a node without updating the changed time use the following code.

$originalChangedTimestamp = $node->getChangedTime();
$node->save();
$node->setChangedTime($originalChangedTimestamp)->save();

This is useful for updating content behind the scenes and not altering the changed time in the admin interface.

This will work with any entity that uses the Drupal\Core\Entity\EntityChangedTrait trait, or extends the Drupal\Core\Entity\EditorialContentEntityBase, which is actually most content entity types in Drupal

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
5 + 15 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.