<p>Since you're listening for an event on an <code>ElementQuery</code>, that is what you're going to get back when calling <code>$event-&gt;sender</code>. Since the <code>EVENT_BEFORE_PREPARE</code> is triggered at the beginning of preparing an element query, there is no element (asset) to speak of yet. Or to put it another way, there is no &quot;target asset&quot; because you're dealing with an asset query, not an asset.</p>

CC BY-SA 4.0
2023-01-20T17:27:27.830Z
Thank you. I am going to look for another workaround.
Kota.M
CC BY-SA 4.0
2023-01-20T01:33:45.957Z
2023-01-20T01:33:45.957Z
Ben Croker
<p>I have been implementing my PHP module for Craft website. Could I ask how can I get the target asset in this if-statement?</p>
<pre><code>protected function initRestrictUserIndex()
{
  Event::on(
    ElementQuery::class,
    ElementQuery::EVENT_BEFORE_PREPARE,
    function(Event $event) {

      if ($event-&gt;sender-&gt;elementType === 'craft\elements\Asset')
      {
        /* MY PROCEDURE */
      }
    }
  )
}
</code></pre>

<craft3><php>
How to get event target asset in PHP module
# How to get event target asset in PHP module

(Asked by: username_0 on 2023-01-19)

I have been implementing my PHP module for Craft website. Could I ask how can I get the target asset in this if-statement?
protected function initRestrictUserIndex()
{
  Event::on(
    ElementQuery::class,
    ElementQuery::EVENT_BEFORE_PREPARE,
    function(Event $event) {

      if ($event->sender->elementType === 'craft\elements\Asset')
      {
        /* MY PROCEDURE */
      }
    }
  )
}

**Tags:** craft3, php


---

## 1 answers

### Answer by username_1 on 2023-01-20. Score: 1

Since you're listening for an event on an `ElementQuery`, that is what you're going to get back when calling `$event->sender`. Since the `EVENT_BEFORE_PREPARE` is triggered at the beginning of preparing an element query, there is no element (asset) to speak of yet. Or to put it another way, there is no "target asset" because you're dealing with an asset query, not an asset.

> **Comments:**
> **username_0**: Thank you. I am going to look for another workaround.  

---

