I'm having a problem calling a service method from another plugin when hooking into the beforeSend
event in the ContactForm plugin. I have the code below in a super simple plugin called ContactFormExtra
to catch if someone wants to also subscribe to our newsletter when they fill in the contact form. I have another subscribe plugin setup with it's service class having the methods to do the subscribing - which also handles a simpler subscribe form without the contact form fields. Any ideas on how this could work - or do I need to put all the subscribe stuff again into the ContactFormExtra
plugin?
This follows the example on GitHub for pixelandtonic/ContactForm
When trying to subscribe on the contact form, I get this error:
Craft\ContactFormExtraPlugin and its behaviors do not have a method or closure named "_getSettings".
Looks like it's trying to call the Subscribe plugin's functions from the ContactFormExtra plugin?
If I don't choose to subscribe and submit the form, then the Subscribe
plugin service call is skipped, moving on to the calls on the ContactFormExtra
plugin's service. Then I get this error:
Property "Craft\WebApp.contactformextra" is not defined.
Simple ContactFormExtra
plugin code:
(plugins/contactformextra/ContactFormExtraPlugin.php
)
class ContactFormExtraPlugin extends BasePlugin
{
public function init()
{
craft()->on('contactForm.beforeSend', function(ContactFormEvent $event) {
$message = $event->params['message'];
$subscribe = craft()->request->getPost('subscribe');
$attribs = $message->getAttributes();
if ($subscribe == 1) {
$result = SubscribeService::subscribe($attribs['fromEmail']);
}
// save the record
$attribs = $message->getAttributes();
$attribs['subscribe'] = $subscribe;
$enquiryModel = craft()->contactformextra->newEnquiry($attribs);
craft()->contactformextra->saveEnquiry($enquiryModel);
});
}
...
}
ContactFormExtra
plugin service code:
(plugins/contactformextra/services/ContactFormExtraService.php
)
class ContactFormExtraService extends BaseApplicationComponent
{
public function newEnquiry($attributes) {
// do stuff here
}
public function saveEnquiry(ContactFormExtra_EnquiryModel &$model) {
// do stuff here
}
}
Subscriber
plugin:
(plugins/subscribe/services/SubscribeService.php
)
class SubscribeService extends BaseApplicationComponent
{
protected $settings;
private function _getSettings()
{
$plugin_settings = craft()->plugins->getPlugin('subscribe')->getSettings();
$this->settings = $plugin_settings;
}
public function subscribe($email)
{
$this->_getSettings();
// do the subscribing here...
}
}