<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>

CC BY-SA 4.0
2021-12-28T20:16:10.950Z
2021-12-28T20:16:10.950Z
iamkeir
<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>

CC BY-SA 4.0
2021-12-27T16:27:28.120Z
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?
MoritzLost
CC BY-SA 4.0
2021-12-28T20:17:56.577Z
@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 :)
iamkeir
<craft3><php><events>
Identify event origin (e.g. via admin vs module code)?
# 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**: @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 :)  

---

## 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.


---

