3

I have two plugins that I want to use in a single form. One is a Guest Entry plugin to save the data submitted in the form, and the other is a Plugin that sends an email notification when the form is submitted.

I want to use both of these in the same form but I'm not sure how. If I have

<input type="hidden" name="action" value="guestEntries/saveEntry">
<input type="hidden" name="action" value="formNotification/getForm">

It won't work since both have the name "action" and I can't change the name or the functions never get called.

What can I do to make both of them work?

0

3 Answers 3

7

It’s not possible for a form to simultaneously invoke two controller actions.

You would need to write a plugin that provides its own controller, which in turn could be used to invoke the other controller actions.

class MyPluginController
{
    public function actionSaveEntryAndSendNotification()
    {
        // Save the entry
        craft()->runController('guestEntries/saveEntry');

        // Send the notification
        craft()->runController('formNotification/getForm');
    }
}

If "FormNotificationController" is already your own custom plugin, then it might make more sense to just call craft()->runController('guestEntries/saveEntry'); from the top of actionGetForm(). Or just save the entry yourself from the controller action – the Guest Entries plugin is really pretty simple; most of the heavy lifting is provided by the EntriesService - saveEntry() API.

0
3

You can't run multiple actions in one request. What you're looking to do is trigger your notification action after the entries.saveEntry event. I'm assuming that your formNotification plugin is a custom plugin that you wrote. Fortunately, events are easy:

craft()->on('entries.saveEntry', function(Event $event) {
    // Your notification code here.
});

The best place to add this event listener is in your plugin's init function. This is located in the main plugin file.

0

How would you write this in Craft CMS 3 / Yii2?

class MyPluginController
{
    public function actionSaveEntryAndSendNotification()
    {
        // Save the entry
        craft()->runController('guestEntries/saveEntry');

        // Send the notification
        craft()->runController('formNotification/getForm');
    }
}
2
  • Think I've found a solution Craft::$app->runAction('guestEntries/saveEntry'); Craft::$app->runAction('formNotification/getForm);
    – mmorkt
    Commented Nov 20, 2020 at 8:37
  • 1
    Want to edit your original post and add your comment as an official answer?
    – Brad Bell
    Commented Nov 20, 2020 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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