Answers.Body: <p>Your code is pretty close. You want to make sure it's a new entry and not a draft or a revision so you could use:</p>
  <pre class="lang-php prettyprint-override"><code>Event::on(
      Entry::class,
      Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {
  
      /** Entry @entry */
      $entry = $event-&gt;sender;
  
      /** @var User $user */
      $user = Craft::$app-&gt;getUser();
  
      if (
          $event-&gt;isNew &amp;&amp; // Makes sure it's a new entry
          $user-&gt;identity-&gt;isInGroup('cook') &amp;&amp; // Makes sure your user is in the cook group
          !ElementHelper::isDraftOrRevision($entry) // Makes it's not a draft or a revision
      ) {  
              $cpUrl = $entry-&gt;cpEditUrl;
              $bodyText = &quot;Dear Admin. /n $userName has created &lt;a href=$cpUrl&gt;$entry&lt;/a&gt;.&quot;;
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-25T15:47:39.513Z
Answers.Text: Ok, that don't work - because the 'cook' can only save drafts - not publish.
Answers.UserDisplayName: Slowwie
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-25T15:50:27.500Z
Answers.Text: Anyway - I found a solution. When I test `$request->isPost` then it works. the initial save event is a get request to the edit site.  Maybe I have to add a conditional for not allow ajax calls and only react on the first post request.
Answers.UserDisplayName: Slowwie
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-25T12:18:31.380Z
Answers.LastActivityDate: 2022-07-25T12:26:51.043Z
Answers.LastEditDate: 2022-07-25T12:26:51.043Z
Answers.UserDisplayName: Oli
Body: <p>I want to send a Mail to an admin, whenever a User of group ‚Cook‘ creates an Entry of Type ‚Recipe‘.</p>
  <p>For that I created an Event reacting on Entry::EVENT_AFTER_SAVE :</p>
  <pre><code>Event::on(
      Entry::class,
      Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {
  
      /** Entry @entry */
      $entry = $event-&gt;sender;
  
      /** @var User $user */
      $user = Craft::$app-&gt;getUser();
  
      if (!$event-&gt;isNew) {
          if ($user-&gt;identity-&gt;isInGroup('cook') &amp;&amp;
              ElementHelper::isDraftOrRevision($entry) &amp;&amp;
              $entry-&gt;getStatus() === Entry::STATUS_DISABLED) {
  
           
              $cpUrl = $entry-&gt;cpEditUrl;
              $bodyText = &quot;Dear Admin. /n $userName has created &lt;a href=$cpUrl&gt;$entry&lt;/a&gt;.&quot;;
          
  
          };
      }
  });
  </code></pre>
  <p>As your can see - I tried a lot to only react on the first creation of the entry. But all this does not the trick.
  Behind the scenes craft created an Entry (and saves it initially) even when the Cook presses the ’New Entry’ Button.</p>
  <p>So I don’t know hook in only when the message is first saved directly by the user pressing the ‚Create‘ Button after entering the content.</p>
  <p>It would be nice if your could help me with that.</p>
  <p>Thanks in advance.
  Michael</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-25T02:19:29.950Z
Comments.Text: You only want this to fire on new entries?
Comments.UserDisplayName: RitterKnight
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-25T03:37:02.553Z
Comments.Text: Yes - the admin just wants to have one email, when the user saved a new recipe first time.
Comments.UserDisplayName: Slowwie
Tags: <craft3><events><element>
Title: Hook into Entry::EVENT_AFTER_SAVE when user saves it
ThreadText: # Hook into Entry::EVENT_AFTER_SAVE when user saves it
  
  (Asked by: username_0 on 2021-01-24)
  
  I want to send a Mail to an admin, whenever a User of group ‚Cook‘ creates an Entry of Type ‚Recipe‘.
  
  For that I created an Event reacting on Entry::EVENT\_AFTER\_SAVE :
  
  ```
  Event::on(
      Entry::class,
      Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {
  
      /** Entry @entry */
      $entry = $event->sender;
  
      /** @var User $user */
      $user = Craft::$app->getUser();
  
      if (!$event->isNew) {
          if ($user->identity->isInGroup('cook') &&
              ElementHelper::isDraftOrRevision($entry) &&
              $entry->getStatus() === Entry::STATUS_DISABLED) {
  
           
              $cpUrl = $entry->cpEditUrl;
              $bodyText = "Dear Admin. /n $userName has created <a href=$cpUrl>$entry</a>.";
          
  
          };
      }
  });
  
  ```
  
  As your can see - I tried a lot to only react on the first creation of the entry. But all this does not the trick.
  Behind the scenes craft created an Entry (and saves it initially) even when the Cook presses the ’New Entry’ Button.
  
  So I don’t know hook in only when the message is first saved directly by the user pressing the ‚Create‘ Button after entering the content.
  
  It would be nice if your could help me with that.
  
  Thanks in advance.
  Michael
  
  **Tags:** craft3, events, element
  
  > **Comments:**
  > **username_1**: You only want this to fire on new entries?  
  > **username_0**: Yes - the admin just wants to have one email, when the user saved a new recipe first time.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-01-25. Score: 3
  
  Your code is pretty close. You want to make sure it's a new entry and not a draft or a revision so you could use:
  
  ```php
  Event::on(
      Entry::class,
      Entry::EVENT_AFTER_SAVE, static function (ModelEvent $event) {
  
      /** Entry @entry */
      $entry = $event->sender;
  
      /** @var User $user */
      $user = Craft::$app->getUser();
  
      if (
          $event->isNew && // Makes sure it's a new entry
          $user->identity->isInGroup('cook') && // Makes sure your user is in the cook group
          !ElementHelper::isDraftOrRevision($entry) // Makes it's not a draft or a revision
      ) {  
              $cpUrl = $entry->cpEditUrl;
              $bodyText = "Dear Admin. /n $userName has created <a href=$cpUrl>$entry</a>.";
      }
  });
  
  ```
  
  > **Comments:**
  > **username_0**: Ok, that don't work - because the 'cook' can only save drafts - not publish.  
  > **username_0**: Anyway - I found a solution. When I test `$request->isPost` then it works. the initial save event is a get request to the edit site.  Maybe I have to add a conditional for not allow ajax calls and only react on the first post request.  
  
  ---
  
  