Save new revision of custom entity I created a custom entity in my module and I can edit and save it just like any other entity in core. I also want to save revisions for this entity but I couldn't find any help so far. I tried this (from node module) but it doesn't work:

$entity = $this-&gt;getEntity();
$entity-&gt;new_revision = true;
$entity-&gt;save();


I have the revisions tables declared in @ContentEntityType annotation, I have the revision_id field declared in baseFieldDefinitions() but I don't know how to actually save a new revision.
For Drupal 8 most intern values like the revision flag (newRevision) is protected, which means that you aren't allowed to set them directly. There is a good reason for this, as Drupal wants to do stuff in some cases when these values are set.

Instead you need to use the ::setNewRevision method on the entity. This will throw an exception if the entity doesn't support revisions. So if you don't know what entities you are dealing with, you could do something like this.

function save_new_revisions(EntityInterface $entities) {
  foreach ($entities as $entity) {
    if ($entity-&gt;getEntityType()-&gt;isRevisionable()) {
      $entity-&gt;setNewRevision();
    }
    $entity-&gt;save();
  }
}