<p>You're importing the wrong class for Users. Try <code>use craft\elements\User</code> instead of <code>use craft\fields\Users</code>. <code>elements\User</code> extends the base <code>elements</code> class which has the event you're trying to use - Here it is in the class reference with an example:</p>
<p><a href="https://docs.craftcms.com/api/v5/craft-base-element.html#event-after-save" rel="nofollow noreferrer">https://docs.craftcms.com/api/v5/craft-base-element.html#event-after-save</a></p>

CC BY-SA 4.0
2024-10-22T16:33:34.857Z
This doesn't work either. Produces error on the calling Event:
   Event::on(Users::class, Users::EVENT_AFTER_SAVE, 
Is there even a Users::EVENT_AFTER_SAVE? I swapped this for element but still the results are that the user data is saved but the module does not catch the event.

Looking here https://docs.craftcms.com/api/v5/craft-elements-user.html#events there is no user save event.
joomkit
CC BY-SA 4.0
2024-10-22T16:54:15.207Z
It inherits from base\Element, so the event should still work. Note that the class is called `User` in the singular, not `Users`. Check for any other typos.
James Smith
CC BY-SA 4.0
2024-10-22T16:58:34.283Z
Thx James - still no dice
joomkit
CC BY-SA 4.0
2024-10-22T17:08:16.333Z
Interesting - its saving and now I am finding user.errors is saying "unable to save your profile" - its coded in template. How can I get the console for user in frontend when they are not privileged i wonder.?
joomkit
CC BY-SA 4.0
2024-10-22T08:11:04.313Z
2024-10-22T08:11:04.313Z
James Smith
<p>I have a module and I want to listen for the saving of a user profile in the front end. Module looks like this.</p>
<pre><code>use Craft;
use yii\base\Event;
use craft\events\UserEvent;
use craft\fields\Users;
use craft\events\ModelEvent;
use yii\base\Module as BaseModule;
</code></pre>
<p>My public init looks like this:</p>
<p>public function init(): void
{
Craft::setAlias('@modules/fetchprofile', <strong>DIR</strong>);</p>
<pre><code>// Set the controllerNamespace based on whether this is a console or web request
if (Craft::$app-&gt;request-&gt;isConsoleRequest) {
    $this-&gt;controllerNamespace = 'modules\\fetchprofile\\console\\controllers';
} else {
    $this-&gt;controllerNamespace = 'modules\\fetchprofile\\controllers';
}

parent::init();

Event::on(
    Users::class, 
    Users::EVENT_AFTER_SAVE, 
     
    function (ModelEvent $event) {
        $user = $event-&gt;sender;        
        Craft::dd($user);
    }
);
</code></pre>
<hr />
<p>When dumping the $user object i get nothing. The ModelEvent function is never hit.
What am I missing?</p>
<h2>Update:
SOLVED via <a href="https://craftcms.stackexchange.com/questions/26797/user-events-in-craft-3">User events in Craft 3</a></h2>
<pre><code> Event::on(
            User::class,
            User::EVENT_AFTER_SAVE,
            function(ModelEvent $event) {
                $user = $event-&gt;sender;        
                Craft::dd($user);
            }
        );
</code></pre>

<users><modules><events>
Problem catching the after save user event in module
# Problem catching the after save user event in module

(Asked by: username_0 on 2024-10-21)

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

**Tags:** users, modules, events

---

## 1 answers

### Answer by username_1 on 2024-10-22. Score: 1

You're importing the wrong class for Users. Try `use craft\elements\User` instead of `use craft\fields\Users`. `elements\User` extends the base `elements` class which has the event you're trying to use - Here it is in the class reference with an example:

<https://docs.craftcms.com/api/v5/craft-base-element.html#event-after-save>

> **Comments:**
> **username_0**: This doesn't work either. Produces error on the calling Event:    Event::on(Users::class, Users::EVENT_AFTER_SAVE,  Is there even a Users::EVENT_AFTER_SAVE? I swapped this for element but still the results are that the user data is saved but the module does not catch the event.  Looking here https://docs.craftcms.com/api/v5/craft-elements-user.html#events there is no user save event.  
> **username_1**: It inherits from base\Element, so the event should still work. Note that the class is called `User` in the singular, not `Users`. Check for any other typos.  
> **username_0**: Thx James - still no dice  
> **username_0**: Interesting - its saving and now I am finding user.errors is saying "unable to save your profile" - its coded in template. How can I get the console for user in frontend when they are not privileged i wonder.?  

---

