Here's an excerpt from a method I use to determine if an entity has changed. I adapted it a little bit to return the names for you. You may need to adapt it more for your specific needs.
mymodule > src > GaryFunctions.php
<?php
/**
* File for holding helper functions user by Gary
*/
namespace Drupal\mymodule;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
class GaryFunctions {
/**
* Check if entity field content has changed
* @param EntityInterface $entity The entity being saved
* @return array A list of changed field names
*/
public function entityHasChanged(EntityInterface $entity) {
$changed_fields = [];
if (!$entity->original) {
return $changed_fields;
}
$field_names = $this->getFieldList($entity->bundle(), $entity->getEntityTypeId());
foreach($field_names as $key => $field_name) {
if($entity->hasField($field_name) && $field_name != 'field_comments' && !$entity->get($field_name)->equals($entity->original->get($field_name))){
$changed_fields[] = $field_name;
// $entity->get($field_name)->getValue();
}
}
return $changed_fields;
}
/**
* Get list of field names from bundle
* @param string $bundle Bundle name
* @return array Array of field names
*/
public function getFieldList($bundle, $entity_type_id) {
$fields_by_weight = [];
$bundle_fields = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load($entity_type_id . '.' . $bundle . '.' . 'default')
->getComponents();
foreach ($bundle_fields as $name => $options) {
$fields_by_weight[] = $name;
}
return $fields_by_weight;
}
}
mymodule > mymodule.module
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\mymodule\GaryFunctions;
function mymodule_entity_presave(EntityInterface $entity) {
$helper = new GaryFunctions;
switch ($entity->bundle()){
case 'projects':
$changed_fields = $helper->entityHasChanged($entity);
if (!empty($changed_fields)) {
print_r($changed_fields);
} else {
print 'didnt change';
}
break;
default:
break;
}
}
array_keys(DiffArray::diffAssocRecursive($node->toArray(), $node->original->toArray())
may give you what you want.