I have a module and I want to listen for the saving of a user profile in the front end. Module looks like this.
use Craft;
use yii\base\Event;
use craft\events\UserEvent;
use craft\fields\Users;
use craft\events\ModelEvent;
use yii\base\Module as BaseModule;
My public init looks like this:
public function init(): void { Craft::setAlias('@modules/fetchprofile', DIR);
// Set the controllerNamespace based on whether this is a console or web request
if (Craft::$app->request->isConsoleRequest) {
$this->controllerNamespace = 'modules\\fetchprofile\\console\\controllers';
} else {
$this->controllerNamespace = 'modules\\fetchprofile\\controllers';
}
parent::init();
Event::on(
Users::class,
Users::EVENT_AFTER_SAVE,
function (ModelEvent $event) {
$user = $event->sender;
Craft::dd($user);
}
);
When dumping the $user object i get nothing. The ModelEvent function is never hit. What am I missing?
Update: SOLVED via User events in Craft 3
Event::on(
User::class,
User::EVENT_AFTER_SAVE,
function(ModelEvent $event) {
$user = $event->sender;
Craft::dd($user);
}
);