How to check if a node entity is a newly save one

When working with entity forms I discovered this interesting piece detecting whether an entity is a newly saved one.

$result = $entity->save();

if ($result == SAVED_NEW) {
    // This is a newly saved one
}
else {
  // This is an updated one
}

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!