Tracking changed fields on users in more detail #13198
-
We're working on a project where Craft will be partly used as a member management system for a medical society, and the client wants to track changes to user profiles. I know there's the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Yeah currently users don’t have any sort of field value change tracking, so you would need to build it yourselves. Here’s a starting point for how you could go about it: use craft\elements\User;
use craft\events\ModelEvent;
use yii\base\Event;
Event::on(User::class, User::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
if ($event->isNew) {
return;
}
/** @var User $user */
$user = $event->sender;
$oldFieldValues = $user->getSerializedFieldValues();
$user->on(User::EVENT_AFTER_SAVE, function() use ($user) {
$newFieldValues = $user->getSerializedFieldValues();
// compare them...
});
}); |
Beta Was this translation helpful? Give feedback.
Yeah currently users don’t have any sort of field value change tracking, so you would need to build it yourselves.
Here’s a starting point for how you could go about it: