<p>Neither <a href="https://craftcms.com/docs/plugins/events-reference#categories-onBeforeDeleteCategory" rel="nofollow noreferrer"><code>onBeforeDeleteCategory</code></a> nor <a href="https://craftcms.com/docs/plugins/events-reference#elements-onBeforeDeleteElements" rel="nofollow noreferrer"><code>onBeforeDeleteElements</code></a> has a <code>$performAction</code> parameter that you can set to cancel the event, which would behavior #2 you're seeing.</p>

<p>The first behavior you're seeing can be explained by <a href="https://craftcms.stackexchange.com/questions/8460/delete-change-status-from-cp-via-ajax-trigger-event/">this</a>, 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.</p>

CC BY-SA 3.0
2017-06-19T22:49:15.317Z
2017-06-19T22:49:15.317Z
Brad Bell
<p>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:</p>

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

CC BY-SA 3.0
2017-06-19T20:56:34.703Z
2017-06-19T20:56:34.703Z
Rick Kukiela
<p>I need to run checks before allowing a user to delete a category. I have added the following code to my main plugin class:</p>

<pre><code>craft()-&gt;on('categories.onBeforeDeleteCategory', function(Event $event) {
    $event-&gt;performAction = false;
});
</code></pre>

<p>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.</p>

<p>There are two problems:</p>

<p>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.</p>

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

<p>Any ideas?</p>

<p>EDIT:</p>

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

<pre><code>craft()-&gt;on('elements.onBeforeDeleteElements', function(Event $event) {
    $event-&gt;performAction = false;
});
</code></pre>

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

<p>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?</p>

<categories><events>
onBeforeDeleteCategory not working / inconsistent
# onBeforeDeleteCategory not working / inconsistent

(Asked by: username_0 on 2017-06-19)

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?

**Tags:** categories, events


---

## 2 answers

### Answer by username_1 on 2017-06-19. Score: 1 | Accepted answer

Neither [`onBeforeDeleteCategory`](https://craftcms.com/docs/plugins/events-reference#categories-onBeforeDeleteCategory) nor [`onBeforeDeleteElements`](https://craftcms.com/docs/plugins/events-reference#elements-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](https://craftcms.stackexchange.com/questions/8460/delete-change-status-from-cp-via-ajax-trigger-event/), 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.


---

### Answer by username_0 on 2017-06-19. Score: 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;
});


---

