0

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
  • As suggested by @Olivier Bon, that way you can restrict the module call – inrsaurabh May 16 '19 at 10:49
2

You could use Craft::$app->getRequest()->getIsCpRequest() (docs) and do something like this in your innit:

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:

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
}
| improve this answer | |
  • Is there a way to actually call this via a form action? Just so it's not actually calling it on every request – JMKelley May 16 '19 at 11:17
  • You will need to test but I guess Craft::$app->getRequest()->getIsPost should work? – Oli May 16 '19 at 12:19
  • How is getIsPost defined? :) – JMKelley May 16 '19 at 13:47
  • That's part of Yii - let me know if that works for you, I'll add it to the original answer :) – Oli May 16 '19 at 14:16
  • is there a way to name it specifically? – JMKelley May 17 '19 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.