verbb / wishlist

A Craft CMS plugin for wishlists for your users to save things to
Other
11 stars 12 forks source link

afterSaveList event doesnt exist/work #102

Closed bymayo closed 2 years ago

bymayo commented 2 years ago

I'm trying to run logic when a wishlist is created by a user. So I tried this code from the docs:

use verbb\wishlist\elements\ListElement;
use yii\base\Event;

Event::on(ListElement::class, ListElement::EVENT_AFTER_SAVE, function(Event $event) {
    $list = $event->sender;
});

But it blocks the list from being created and errors.

When I tried to find the ListElement::EVENT_AFTER_SAVE event in the plugin, this doesn't exist anywhere. So my thinking is that this is in the docs, but not in the plugin?!

The only one I could find was for ListTypes::EVENT_AFTER_SAVE_LISTTYPE

None of the other events seem to work

Craft CMS 3.7.21 Pro Wishlist 1.4.12

engram-design commented 2 years ago

The EVENT_AFTER_SAVE event is inherited from Craft's base Element class.

But it blocks the list from being created and errors.

Can you clarify what you mean here? What about it blocks the list from being created, and what errors do you get?

bymayo commented 2 years ago

When I add that code in the first post, it just doesn't create the list or add any items to the list.

If I remove the code, it does?!

Really odd.

I checked the logs and no errors are being logged from what I can see

engram-design commented 2 years ago

That doesn't make sense, as that's the after-save event, there's nothing in there that would prevent it from saving, unless it's a fatal error (which would log). I've even just tested that code in my module saving from the control panel and front end with no issue.

Even if you add a Craft::dd('test') or similar inside the event callback - does that print? Or adding that before or after the event code, does that print?

engram-design commented 2 years ago

I don't think it'd matter, but technically the type returned in the callback is a craft\events\ModelEvent.

use craft\events\ModelEvent;
use verbb\wishlist\elements\ListElement;
use yii\base\Event;

Event::on(ListElement::class, ListElement::EVENT_AFTER_SAVE, function(ModelEvent $event) {
    $list = $event->sender;
});
bymayo commented 2 years ago

Dammit, it turns out another AFTER_SAVE event in my module was conflicting with it somehow. I removed all other events from the file and this one runs fine.