0

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...
  }

}
1

Capitalization matters when you're calling services. Try updating this line:

$enquiryModel = craft()->contactformextra->newEnquiry($attribs);

to this:

$enquiryModel = craft()->contactFormExtra->newEnquiry($attribs);

and make sure you update that across all the places where you're calling that service. Try that, and post the results. I'll take another look if you're still seeing the issue, which may still be there; just grabbing at low-hanging fruit right now. :)

| improve this answer | |
  • Thanks so much Taylor that was it - what a silly mistake! I think maybe for some reason I thought the service name was meant to be all lower like the plugin folder name. – moldedjelly Sep 18 '15 at 0:32
0

I don't quite understand why you're getting the error message you are, but I see a couple of issues with your code that should be addressed first.

  1. You're calling SubscribeService::subscribe statically and the method is not declared as a static method in SubscribeService (public function subscribe($email)).

  2. Generally you don't want expose static methods in the service layer of your plugin. If that's the only service your plugin has, then you should be able to do a craft()->subscribe->subscribe($attribs['fromEmail']) from your ContactFormExtra plugin.

| improve this answer | |
  • Thanks for the tips Brad - my bad on the static thing. If I keep that method as public and change the ContactFormExtra plugin to call the method via craft()->subscribe->subscribe($attribs['fromEmail']) I get the following error: Property "Craft\WebApp.subscribe" is not defined. Could this be related to the init() limitations discussed here? ie. the service in the Subscribe plugin isn't registered yet? – moldedjelly Sep 17 '15 at 5:46
  • I've updated my question - firstly moving the subscribe form field stuff into the ContactFormExtra plugin so I don't edit the ContactForm plugin. I've added the 2nd part of the ContactFormExtra functionality which is to save the enquiry and I've used craft()->contactformextra->... to make the call and I still run into an error when submitting the form without ticking subscribe - Property "Craft\WebApp.contactformextra" is not defined. – moldedjelly Sep 17 '15 at 13:03

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.