Answers.Body: <p>You could use <code>Craft::$app-&gt;getRequest()-&gt;getIsCpRequest()</code> (<a href="https://docs.craftcms.com/api/v3/craft-console-request.html#iscprequest" rel="nofollow noreferrer">docs</a>) and do something like this in your <code>innit</code>:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function init()
  {
      parent::init();
  
      if (!Craft::$app-&gt;getRequest()-&gt;getIsCpRequest()) {
          // Listens for EVENT_AFTER_SAVE_ELEMENT and element instanceof Entry
          self::newEntryElementIsSaved();
      }
  }
  </code></pre>
  
  <p>Then further down add:</p>
  
  <pre class="lang-php prettyprint-override"><code>protected function newEntryElementIsSaved()
  {
      Event::on(
          Elements::class,
          Elements::EVENT_AFTER_SAVE_ELEMENT,
          function (ElementEvent $event){
              // Get the correct Entry that's currently saving
              if ($event-&gt;element instanceof craft\elements\Entry) {
                  $entry = $event-&gt;element;
                  // Set $field to the Field Value
                  $field =  $entry-&gt;getFieldValue('proposalStatus');
                  // Set the Proposals &gt; Campaign Authors Email
                  $owner =  $entry-&gt;getFieldValue('proposalCampaign')-&gt;first()-&gt;getAuthor()-&gt;emailAddress;
  
                  if($field == 'delivered') {
                    return Craft::$app
                        -&gt;getMailer()
                        -&gt;compose()
                        -&gt;setFrom('test@test.com')
                        -&gt;setTo($owner)
                        -&gt;setSubject('Proposal Delivered!')
                        -&gt;send();
                  }
              }
          } // Close Function
      ); // Close Event
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-16T11:17:43.527Z
Answers.Text: Is there a way to actually call this via a form action? Just so it's not actually calling it on every request
Answers.UserDisplayName: JMKelley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-16T12:19:48.207Z
Answers.Text: You will need to test but I guess `Craft::$app->getRequest()->getIsPost` should work?
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-16T13:47:39.143Z
Answers.Text: How is `getIsPost` defined? :)
Answers.UserDisplayName: JMKelley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-16T14:16:27.187Z
Answers.Text: That's [part of Yii](https://www.yiiframework.com/doc/api/2.0/yii-web-request#getIsPost()-detail) - let me know if that works for you, I'll add it to the original answer :)
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-17T11:15:01.060Z
Answers.Text: is there a way to name it specifically?
Answers.UserDisplayName: JMKelley
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-16T09:59:36.267Z
Answers.LastActivityDate: 2019-05-16T09:59:36.267Z
Answers.UserDisplayName: Oli
Body: <p>I'm looking to call the following Module function from a front end form. I've looked through numerous answers but I can't seem to find a definitive answer on this. </p>
  
  <p>Here's my Module's code, but obviously I don't want this to run every time and entry get's saved, but when the form on the front-end calls it. </p>
  
  <pre><code>&lt;?php
  
  namespace modules;
  
  use Craft;
  
  use yii\base\Module;
  use yii\base\Event;
  use craft\services\Elements;
  
  class ShifterModule extends Module
  {
      public function init()
      {
  
          parent::init();
  
          Event::on(
              Elements::class,
              Elements::EVENT_AFTER_SAVE_ELEMENT,
              function (ElementEvent $event){
                  // Get the correct Entry that's currently saving
                  if ($event-&gt;element instanceof craft\elements\Entry) {
                      $entry = $event-&gt;element;
                      // Set $field to the Field Value
                      $field =  $entry-&gt;getFieldValue('proposalStatus');
                      // Set the Proposals &gt; Campaign Authors Email
                      $owner =  $entry-&gt;getFieldValue('proposalCampaign')-&gt;first()-&gt;getAuthor()-&gt;emailAddress;
  
                      if($field == 'delivered'){
                        return Craft::$app
                            -&gt;getMailer()
                            -&gt;compose()
                            -&gt;setFrom('test@test.com')
                            -&gt;setTo($owner)
                            -&gt;setSubject('Proposal Delivered!')
                            -&gt;send();
                      }
                  }
              } // Close Function
          ); // Close Event
      } // Close Public Function
  } // Close Module Off
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-16T10:49:16.420Z
Comments.Text: As suggested by @Olivier Bon, that way you can restrict the module call
Comments.UserDisplayName: inrsaurabh
Tags: <php><modules>
Title: Call Module Function from Front End
ThreadText: # Call Module Function from Front End
  
  (Asked by: username_0 on 2019-05-14)
  
  I'm looking to call the following Module function from a front end form. I've looked through numerous answers but I can't seem to find a definitive answer on this.
  
  Here's my Module's code, but obviously I don't want this to run every time and entry get's saved, but when the form on the front-end calls it.
  
  ```
  <?php
  
  namespace modules;
  
  use Craft;
  
  use yii\base\Module;
  use yii\base\Event;
  use craft\services\Elements;
  
  class ShifterModule extends Module
  {
      public function init()
      {
  
          parent::init();
  
          Event::on(
              Elements::class,
              Elements::EVENT_AFTER_SAVE_ELEMENT,
              function (ElementEvent $event){
                  // Get the correct Entry that's currently saving
                  if ($event->element instanceof craft\elements\Entry) {
                      $entry = $event->element;
                      // Set $field to the Field Value
                      $field =  $entry->getFieldValue('proposalStatus');
                      // Set the Proposals > Campaign Authors Email
                      $owner =  $entry->getFieldValue('proposalCampaign')->first()->getAuthor()->emailAddress;
  
                      if($field == 'delivered'){
                        return Craft::$app
                            ->getMailer()
                            ->compose()
                            ->setFrom('test@test.com')
                            ->setTo($owner)
                            ->setSubject('Proposal Delivered!')
                            ->send();
                      }
                  }
              } // Close Function
          ); // Close Event
      } // Close Public Function
  } // Close Module Off
  
  ```
  
  **Tags:** php, modules
  
  > **Comments:**
  > **username_1**: As suggested by @Olivier Bon, that way you can restrict the module call  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-05-16. Score: 2
  
  You could use `Craft::$app->getRequest()->getIsCpRequest()` ([docs](https://docs.craftcms.com/api/v3/craft-console-request.html#iscprequest)) and do something like this in your `innit`:
  
  ```php
  public function init()
  {
      parent::init();
  
      if (!Craft::$app->getRequest()->getIsCpRequest()) {
          // Listens for EVENT_AFTER_SAVE_ELEMENT and element instanceof Entry
          self::newEntryElementIsSaved();
      }
  }
  
  ```
  
  Then further down add:
  
  ```php
  protected function newEntryElementIsSaved()
  {
      Event::on(
          Elements::class,
          Elements::EVENT_AFTER_SAVE_ELEMENT,
          function (ElementEvent $event){
              // Get the correct Entry that's currently saving
              if ($event->element instanceof craft\elements\Entry) {
                  $entry = $event->element;
                  // Set $field to the Field Value
                  $field =  $entry->getFieldValue('proposalStatus');
                  // Set the Proposals > Campaign Authors Email
                  $owner =  $entry->getFieldValue('proposalCampaign')->first()->getAuthor()->emailAddress;
  
                  if($field == 'delivered') {
                    return Craft::$app
                        ->getMailer()
                        ->compose()
                        ->setFrom('test@test.com')
                        ->setTo($owner)
                        ->setSubject('Proposal Delivered!')
                        ->send();
                  }
              }
          } // Close Function
      ); // Close Event
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Is there a way to actually call this via a form action? Just so it's not actually calling it on every request  
  > **username_2**: You will need to test but I guess `Craft::$app->getRequest()->getIsPost` should work?  
  > **username_0**: How is `getIsPost` defined? :)  
  > **username_2**: That's [part of Yii](https://www.yiiframework.com/doc/api/2.0/yii-web-request#getIsPost()-detail) - let me know if that works for you, I'll add it to the original answer :)  
  > **username_0**: is there a way to name it specifically?  
  
  ---
  
  