Answers.Body: <p>It appears that when one or more Entries (or other types of content) are manipulated (in my case, deleted) from the bulk edit view in the Control Panel, the type of events that are triggered are <strong>Element</strong> events, rather than <strong>Entry</strong> events. </p>
  
  <p>If you refer to the <a href="https://craftcms.com/docs/plugins/events-reference#elements-onPerformAction" rel="noreferrer">Craft documentation on elements.onPerformAction</a>, you can see that the 2 params you get access to are <strong>action</strong> and <strong>criteria</strong>.</p>
  
  <p>Here's how I was able to find what I needed when working with elements.onPerformAction and the elements.onBeforePerformAction events.</p>
  
  <ol>
  <li><strong>What type of action is being performed on the element? (Save, Delete, etc.)</strong></li>
  </ol>
  
  <p>I was able to capture what type of action was being being performed on my element by looking at the "action" param of the event. Using the following code output the action that was taking place:</p>
  
  <pre><code>$action = $event-&gt;params['action']-&gt;classHandle; //Outputs 'Delete'
  </code></pre>
  
  <p>This bit of code will output the string 'Delete'. Perfect.</p>
  
  <ol start="2">
  <li><strong>How do I identify the entry that the action is being performed on.</strong></li>
  </ol>
  
  <p>What I learned is that, when working with an Element event, you're not just working with a single entry. You're potentially working with many Entries, or rather "Elements" to be more specific. Because of this, you can't just grab the Element being manipulated in the same style you can with an Entries event. </p>
  
  <p><code>$entry = $event-&gt;params['entry'];</code> won't work here. </p>
  
  <p>This brings us to the "criteria" param of the Element event.</p>
  
  <p>You can simply grab the "criteria" param of the event to get the elements that the event is taking place on. This param contains an ElementCriteriaModel object.</p>
  
  <p>Viewing the <a href="https://craftcms.com/classreference/models/ElementCriteriaModel" rel="noreferrer">class reference for ElementCriteriaModel</a> shows us what we can do with this object. In my case, I can simply use <code>-&gt;find()</code> on it to get all of the elements I'm working with.</p>
  
  <pre><code>$elements = $event-&gt;params['criteria']-&gt;find();
  </code></pre>
  
  <p>I now have an array of the Elements having actions taken on them, stored in $entries.</p>
  
  <p>Now that I have both the type of action and the elements, I have what I was needing to move forward.</p>
  
  <p>Here are my event listeners in full context. One handles a <code>entries.deleteEntry</code> event, and the other handles a <code>elements.onBeforePerformAction</code> event.</p>
  
  <pre><code>craft()-&gt;on('entries.deleteEntry', function(Event $event) {
      $entry = $event-&gt;params['entry'];
      craft()-&gt;lhngroups_organizations-&gt;deleteOrgCustomerAddress($entry);
  });
  
  craft()-&gt;on('elements.onBeforePerformAction', function(Event $event) {
      $action = $event-&gt;params['action']-&gt;classHandle; //Outputs 'Delete'
      $elements = $event-&gt;params['criteria']-&gt;find(); //Array of Elements
      if ($action == 'Delete') {
          foreach ($elements as $element) {
              craft()-&gt;lhngroups_organizations-&gt;deleteOrgCustomerAddress($element);
          }
      }
  });
  </code></pre>
  
  <p>It's important to remember that since we could theoretically be working with many types of Elements (Entries, Assets, Categories, Users, etc.), rather than <em>just</em> Entries, we may need to validate that we're working on the right <em>types</em> of Elements before we operate on them. Since I have multiple event handlers calling my <code>deleteOrgCustomerAddress()</code> service, I did this validation check there.</p>
  
  <pre><code>public function deleteOrgCustomerAddress($element)
  {
  
      // Make sure we're working with an entry in the organizations section
      if ($element-&gt;elementType !== 'Entry' &amp;&amp; $element-&gt;section-&gt;handle !== 'organizations') {
          return;
      }
  
      // We are good to go! Do stuff with the Entry...
  }
  </code></pre>
  
  <p>Thanks to @lindsey-d for some help along the way!</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-17T21:48:50.930Z
Answers.Text: Wow, that's way better than what I was going to answer. :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-17T14:41:07.533Z
Answers.LastActivityDate: 2016-09-17T23:14:45.253Z
Answers.LastEditDate: 2016-09-17T23:14:45.253Z
Answers.UserDisplayName: jthomas
Body: <p>I have a plugin that needs to do something when an entry is deleted. I've recently discovered that the <a href="https://craftcms.stackexchange.com/questions/8460/delete-change-status-from-cp-via-ajax-trigger-event/14612#14612">entries.deleteEntry event is not fired when deleting bulk entries</a> (the list view with checkboxes) in the Control Panel (CP). Instead, an <a href="https://craftcms.com/docs/plugins/events-reference#elements-onPerformAction" rel="nofollow noreferrer">elements.onPerformAction</a> event is fired (you can also listen for onBeforePerformAction). </p>
  
  <p>Now that I know that these are the events that are fired when deleting an entry from the list view in the CP, and that I'm working with an Element event instead of a Entry event, I'm struggling to access the type of information I need.</p>
  
  <p>I would like to know...</p>
  
  <ol>
  <li><p>what type of action is being performed on the element? (Save, Delete, etc.)</p></li>
  <li><p>how do I identify the entry that the action is being performed on?</p></li>
  </ol>
  
Tags: <plugin-development><elementcriteriamodel><elementaction>
Title: Get the type of action and the elements(s) that an action is being performed on when listening to elements.onPerformAction?
ThreadText: # Get the type of action and the elements(s) that an action is being performed on when listening to elements.onPerformAction?
  
  (Asked by: username_0 on 2016-09-17)
  
  I have a plugin that needs to do something when an entry is deleted. I've recently discovered that the [entries.deleteEntry event is not fired when deleting bulk entries](https://craftcms.stackexchange.com/questions/8460/delete-change-status-from-cp-via-ajax-trigger-event/14612#14612) (the list view with checkboxes) in the Control Panel (CP). Instead, an [elements.onPerformAction](https://craftcms.com/docs/plugins/events-reference#elements-onPerformAction) event is fired (you can also listen for onBeforePerformAction).
  
  Now that I know that these are the events that are fired when deleting an entry from the list view in the CP, and that I'm working with an Element event instead of a Entry event, I'm struggling to access the type of information I need.
  
  I would like to know...
  
  1. what type of action is being performed on the element? (Save, Delete, etc.)
  2. how do I identify the entry that the action is being performed on?
  
  **Tags:** plugin-development, elementcriteriamodel, elementaction
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-09-17. Score: 9
  
  It appears that when one or more Entries (or other types of content) are manipulated (in my case, deleted) from the bulk edit view in the Control Panel, the type of events that are triggered are **Element** events, rather than **Entry** events.
  
  If you refer to the [Craft documentation on elements.onPerformAction](https://craftcms.com/docs/plugins/events-reference#elements-onPerformAction), you can see that the 2 params you get access to are **action** and **criteria**.
  
  Here's how I was able to find what I needed when working with elements.onPerformAction and the elements.onBeforePerformAction events.
  
  1. **What type of action is being performed on the element? (Save, Delete, etc.)**
  
  I was able to capture what type of action was being being performed on my element by looking at the "action" param of the event. Using the following code output the action that was taking place:
  
  ```
  $action = $event->params['action']->classHandle; //Outputs 'Delete'
  
  ```
  
  This bit of code will output the string 'Delete'. Perfect.
  
  2. **How do I identify the entry that the action is being performed on.**
  
  What I learned is that, when working with an Element event, you're not just working with a single entry. You're potentially working with many Entries, or rather "Elements" to be more specific. Because of this, you can't just grab the Element being manipulated in the same style you can with an Entries event.
  
  `$entry = $event->params['entry'];` won't work here.
  
  This brings us to the "criteria" param of the Element event.
  
  You can simply grab the "criteria" param of the event to get the elements that the event is taking place on. This param contains an ElementCriteriaModel object.
  
  Viewing the [class reference for ElementCriteriaModel](https://craftcms.com/classreference/models/ElementCriteriaModel) shows us what we can do with this object. In my case, I can simply use `->find()` on it to get all of the elements I'm working with.
  
  ```
  $elements = $event->params['criteria']->find();
  
  ```
  
  I now have an array of the Elements having actions taken on them, stored in $entries.
  
  Now that I have both the type of action and the elements, I have what I was needing to move forward.
  
  Here are my event listeners in full context. One handles a `entries.deleteEntry` event, and the other handles a `elements.onBeforePerformAction` event.
  
  ```
  craft()->on('entries.deleteEntry', function(Event $event) {
      $entry = $event->params['entry'];
      craft()->lhngroups_organizations->deleteOrgCustomerAddress($entry);
  });
  
  craft()->on('elements.onBeforePerformAction', function(Event $event) {
      $action = $event->params['action']->classHandle; //Outputs 'Delete'
      $elements = $event->params['criteria']->find(); //Array of Elements
      if ($action == 'Delete') {
          foreach ($elements as $element) {
              craft()->lhngroups_organizations->deleteOrgCustomerAddress($element);
          }
      }
  });
  
  ```
  
  It's important to remember that since we could theoretically be working with many types of Elements (Entries, Assets, Categories, Users, etc.), rather than *just* Entries, we may need to validate that we're working on the right *types* of Elements before we operate on them. Since I have multiple event handlers calling my `deleteOrgCustomerAddress()` service, I did this validation check there.
  
  ```
  public function deleteOrgCustomerAddress($element)
  {
  
      // Make sure we're working with an entry in the organizations section
      if ($element->elementType !== 'Entry' && $element->section->handle !== 'organizations') {
          return;
      }
  
      // We are good to go! Do stuff with the Entry...
  }
  
  ```
  
  Thanks to @lindsey-d for some help along the way!
  
  > **Comments:**
  > **username_1**: Wow, that's way better than what I was going to answer. :)  
  
  ---
  
  