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->getEntity();
$entity->new_revision = true;

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.

1 Answer 1


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->getEntityType()->isRevisionable()) {
  • I had to add $entity->setNewRevision() instead of $entity->new_revision = true; Oct 9, 2016 at 11:05
  • Good answer. FYI, you might as well set a log message as well. For example: $entity->setRevisionLogMessage('Transitioned by SendToPayroll service.'); Nov 9, 2018 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.