Skip to content

Instantly share code, notes, and snippets.

@stovak
Created January 15, 2014 21:22
Show Gist options
  • Save stovak/8444958 to your computer and use it in GitHub Desktop.
Save stovak/8444958 to your computer and use it in GitHub Desktop.
function nebula_audit_entity_update($entity, $type) {
$orig_wrapper = entity_metadata_wrapper($type, $entity->original);
$new_wrapper = entity_metadata_wrapper($type, $entity);
$properties = $orig_wrapper->getPropertyInfo();
$diffs = array();
foreach ($properties as $key => $property) {
$old = $orig_wrapper->$key->value();
$new = $new_wrapper->$key->value();
if ($old !== $new) {
if (is_array($old)) {
$diff = $property['label']." changed:<ul>";
$added = array_diff($old, $new);
foreach ($added as $item) {
$diff .= "<li>".(string)$item." added</li>";
}
$subtracted = array_diff($new, $old);
foreach($subtracted as $item) {
$diff .= "<li>".(string)$item." removed</li>";
}
$diff .="</ul>";
} else {
$diff = $property['label']." changed from '{$old}' to '${new}'";
}
}
if (!empty($diff)) {
$diffs[] = $diff;
unset($diff);
}
}
$diffs = "<ul><li>" . implode("</li><li>", $diffs) . "</li><ul><br><br>";
watchdog("nebula_audit", "Entity {$type}://{$entity} updated by {$GLOBALS['user']->mail}: {$diffs}", array(), WATCHDOG_INFO );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment