Answers.Body: <p><a href="https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module#2-the-module-class" rel="nofollow noreferrer">Discovered here</a> you can use <code>Craft::$app-&gt;getRequest()-&gt;getIsCpRequest()</code> to check if it was a request via control panel.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-28T20:16:10.950Z
Answers.LastActivityDate: 2021-12-28T20:16:10.950Z
Answers.UserDisplayName: iamkeir
Body: <p>When using Craft Event triggers in PHP (e.g. <code>Event::on</code>), is there any event metadata that can tell where the event originated from? E.g. if it was triggered from an admin action, or if it was triggered by module code (e.g. <code>saveElement</code>)?</p>
  <p>Perhaps something in the <code>$event</code> object?</p>
  <pre><code>Event::on(
    Entry::class,
      Entry::EVENT_BEFORE_SAVE,
        function(ModelEvent $event) {
          $entry = $event-&gt;sender;
  </code></pre>
  <p>Thanks!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-27T16:27:28.120Z
Comments.Text: Why do you need to know what triggered the event? It's likely you won't cover all possible scenarios if you make distinctions like that. What problem are you actually trying to solve?
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-28T20:17:56.577Z
Comments.Text: @MoritzLost hard to expand fully as it is a complex issue, but we were getting issues with saving matrix data via `EVENT_BEFORE_SAVE`, so we were considering using `saveElement` and `EVENT_AFTER_SAVE`, but, as expected, had an infinite loop of saving. Was exploring all options, including being able to tell difference between save type. However, looks like we will abandon this approach for reasons you indicate. Thanks for reply :)
Comments.UserDisplayName: iamkeir
Tags: <craft3><php><events>
Title: Identify event origin (e.g. via admin vs module code)?
ThreadText: # Identify event origin (e.g. via admin vs module code)?
  
  (Asked by: username_0 on 2021-12-23)
  
  When using Craft Event triggers in PHP (e.g. `Event::on`), is there any event metadata that can tell where the event originated from? E.g. if it was triggered from an admin action, or if it was triggered by module code (e.g. `saveElement`)?
  
  Perhaps something in the `$event` object?
  
  ```
  Event::on(
    Entry::class,
      Entry::EVENT_BEFORE_SAVE,
        function(ModelEvent $event) {
          $entry = $event->sender;
  
  ```
  
  Thanks!
  
  **Tags:** craft3, php, events
  
  > **Comments:**
  > **username_1**: Why do you need to know what triggered the event? It's likely you won't cover all possible scenarios if you make distinctions like that. What problem are you actually trying to solve?  
  > **username_0**: @username_1 hard to expand fully as it is a complex issue, but we were getting issues with saving matrix data via `EVENT_BEFORE_SAVE`, so we were considering using `saveElement` and `EVENT_AFTER_SAVE`, but, as expected, had an infinite loop of saving. Was exploring all options, including being able to tell difference between save type. However, looks like we will abandon this approach for reasons you indicate. Thanks for reply :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-12-28. Score: 4 | Accepted answer
  
  [Discovered here](https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module#2-the-module-class) you can use `Craft::$app->getRequest()->getIsCpRequest()` to check if it was a request via control panel.
  
  
  ---
  
  