verbb / wishlist

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

Set status on creating list in frontend #130

Closed espensgr closed 6 months ago

espensgr commented 7 months ago

Question

I'm trying to add the possibility to enable or disable a list when a user is creating it in the frontend. I have tried setting an enabled param in the ajax call. Is there a way i ca n do this, or do i have to add some custom stuff to the beforeSaveList event?

Additional context

No response

engram-design commented 7 months ago

There aren't any front-end facing events that will allow you to set the enabled state of a list, but we'll look at adding that support.

You can handle this yourself with PHP events in a module if you like:

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

Event::on(ListElement::class, ListElement::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    $list = $event->sender;
    $event->isValid = false;

    if (Craft::$app->getRequest()->getParam('myParam')) {
        $event->sender->enabled = false;
    }
});

Where, you can check for the presence of a param you specify, to enable or disable the list when it's saved.

espensgr commented 7 months ago

Nice! got it working with this

Event::on( ListElement::class, ListElement::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    $list = $event->sender;
    $list->enabled = filter_var( Craft::$app->getRequest()->getParam( 'enabled' ) ?? false, FILTER_VALIDATE_BOOLEAN );
} );

But came across another "bug". After i have made a unabled list, i cannot delete it in frontend, i get this error: error: "No list exists with the ID “888”." I have the Manage Disabled Lists setting turned on.

engram-design commented 7 months ago

Fixed for the next release. To get this early, run composer require verbb/wishlist:"dev-craft-4 as 2.0.6

espensgr commented 7 months ago

Nice, thanks!

engram-design commented 6 months ago

Fixed in 2.0.7