Skip to content

Tracking changed fields on users in more detail #13198

Closed Answered by brandonkelly
janhenckens asked this question in Q&A
Discussion options

You must be logged in to vote

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...
    });
});

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@janhenckens
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants