Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-20T17:27:27.830Z
Answers.Text: Thank you. I am going to look for another workaround.
Answers.UserDisplayName: Kota.M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-20T01:33:45.957Z
Answers.LastActivityDate: 2023-01-20T01:33:45.957Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Tags: <craft3><php>
Title: How to get event target asset in PHP module
ThreadText: # 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.  
  
  ---
  
  