1

I need to run checks before allowing a user to delete a category. I have added the following code to my main plugin class:

craft()->on('categories.onBeforeDeleteCategory', function(Event $event) {
    $event->performAction = false;
});

This is just to test it. I expect this to cause any category delete functions to fail because I'm explicitly setting the event perform action property to false.

There are two problems:

No. 1 - This code only runs when I click into the category and then press the delete button. If you do it from the action menu in the category list this event is not even fired. This seems like a bug to me.

No. 2 - This code, even when executed, does not prevent the category from being deleted. Even though I set $event->performAction = false it still just deletes the category.

Any ideas?

EDIT:

I did some sleuthing regarding #1 and I found out that I have to call the event like this:

craft()->on('elements.onBeforeDeleteElements', function(Event $event) {
    $event->performAction = false;
});

However I'm still running into #2 on both instances where setting performAction to false does not prevent the action from completing... :(

Edit 2 It looks like there is no condition for performAction after the event is fired so there appears to be no way to stop a delete event via an event. Is this the case?

1

Neither onBeforeDeleteCategory nor onBeforeDeleteElements has a $performAction parameter that you can set to cancel the event, which would behavior #2 you're seeing.

The first behavior you're seeing can be explained by this, which you've already found the workaround for. FWIW, that's changed for the upcoming Craft which, which behaves more as you'd expect now.

0

Honestly I'm not sure if this is the best way but I found a work-around that seems to do the job. I have to explicitly send the failed response and exit the execution from within my event. My test case now works from both screens using this code:

craft()->on('elements.onBeforeDeleteElements', function(Event $event) {
    if(craft()->request->isAjaxRequest()) {
        craft()->controller->returnJson(['success' => false, 'message' => "unable to delete"]);
    } else {
        craft()->userSession->setError("Unable to delete");
        craft()->controller->redirectToPostedUrl();
    }
    exit;
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.