In code, only create a revision if a field is changed I have a custom revisionable entity with a few dozen fields. I'm importing data about the entity on a periodic basis. If the entity has changed, I'd like to save it and create a new revision. 

$dogs = \Drupal::entityTypeManager()
  -&gt;getStorage('my_entity')
  -&gt;loadByProperties(['my_entity_id' =&gt; $my_entity_id]);

$dog = reset($dogs);

$dog-&gt;set('name', $record['TheName']);
$dog-&gt;set('birth_date', $record['Dog Birth Date']);
// Lots more -&gt;set happening here.
$dog-&gt;set('notes', $record['Notes']);
if ($dog-&gt;someMagicComparisonFunction()) {
  $dog-&gt;setNewRevision(TRUE);
  $dog-&gt;save();
}


I searched for the someMagicComparisonFunction but didn't find it. Does it exist?
This may do the trick in a hook_entity_presave(), or get you close:

$old_dog = clone $dog;
update_the_dog($dog);

// Use == to allow for out of order keys.
$same = $dog-&gt;toArray() == $old_dog-&gt;toArray();


This is kinda of cheating, and you may want to filter the arrays for just the fields and not the properties.  It also isn't multilingual-safe.

To do it properly, start reading ContentEntityBase::hasTranslationChanges() and FieldItemList::equals().  The code above makes a lot of assumptions about fields and storage, which ContentEntityBase() does properly.