Answers.Body: <p>Capitalization matters when you're calling services. Try updating this line:</p>
  
  <p><code>$enquiryModel = craft()-&gt;contactformextra-&gt;newEnquiry($attribs);</code></p>
  
  <p>to this:</p>
  
  <p><code>$enquiryModel = craft()-&gt;contactFormExtra-&gt;newEnquiry($attribs);</code></p>
  
  <p>and make sure you update that across <strong>all</strong> 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. :)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-18T00:32:25.627Z
Answers.Text: 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.
Answers.UserDisplayName: moldedjelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-17T17:28:12.667Z
Answers.LastActivityDate: 2015-09-17T17:28:12.667Z
Answers.UserDisplayName: Taylor Daughtry
Answers.Body: <p>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.</p>
  
  <ol>
  <li><p>You're calling <code>SubscribeService::subscribe</code> statically and the method is not declared as a static method in SubscribeService (<code>public function subscribe($email)</code>).</p></li>
  <li><p>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 <code>craft()-&gt;subscribe-&gt;subscribe($attribs['fromEmail'])</code> from your <code>ContactFormExtra</code> plugin.</p></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-17T05:46:18.380Z
Answers.Text: 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](http://craftcms.stackexchange.com/questions/847/error-when-calling-methods-between-two-services-triggered-by-init)?
  
  ie. the service in the `Subscribe` plugin isn't registered yet?
Answers.UserDisplayName: moldedjelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-17T13:03:50.953Z
Answers.Text: 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.`
Answers.UserDisplayName: moldedjelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-17T05:29:09.690Z
Answers.LastActivityDate: 2015-09-17T05:29:09.690Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm having a problem calling a service method from another plugin when hooking into the <code>beforeSend</code> event in the <a href="https://github.com/pixelandtonic/ContactForm" rel="nofollow">ContactForm</a> plugin. I have the code below in a super simple plugin called <code>ContactFormExtra</code> 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 <code>ContactFormExtra</code> plugin?</p>
  
  <p>This follows the example on GitHub for <code>pixelandtonic/ContactForm</code></p>
  
  <p>When trying to subscribe on the contact form, I get this error:</p>
  
  <pre><code>Craft\ContactFormExtraPlugin and its behaviors do not have a method or closure named "_getSettings". 
  </code></pre>
  
  <p>Looks like it's trying to call the Subscribe plugin's functions from the ContactFormExtra plugin?</p>
  
  <p>If I don't choose to subscribe and submit the form, then the <code>Subscribe</code> plugin service call is skipped, moving on to the calls on the <code>ContactFormExtra</code> plugin's service. Then I get this error:</p>
  
  <pre><code>Property "Craft\WebApp.contactformextra" is not defined.
  </code></pre>
  
  <p>Simple <code>ContactFormExtra</code> plugin code: 
  (<code>plugins/contactformextra/ContactFormExtraPlugin.php</code>)</p>
  
  <pre><code>class ContactFormExtraPlugin extends BasePlugin
  {
    public function init()
    {
      craft()-&gt;on('contactForm.beforeSend', function(ContactFormEvent $event) {
        $message = $event-&gt;params['message'];
        $subscribe = craft()-&gt;request-&gt;getPost('subscribe');
        $attribs = $message-&gt;getAttributes();
        if ($subscribe == 1) {
          $result = SubscribeService::subscribe($attribs['fromEmail']);
        }
        // save the record
        $attribs = $message-&gt;getAttributes();
        $attribs['subscribe'] = $subscribe;
        $enquiryModel = craft()-&gt;contactformextra-&gt;newEnquiry($attribs);
        craft()-&gt;contactformextra-&gt;saveEnquiry($enquiryModel);
      });
    }
    ...
  }
  </code></pre>
  
  <p><code>ContactFormExtra</code> plugin service code: 
  (<code>plugins/contactformextra/services/ContactFormExtraService.php</code>)</p>
  
  <pre><code>class ContactFormExtraService extends BaseApplicationComponent
  {
  
      public function newEnquiry($attributes) {
        // do stuff here
      }
  
      public function saveEnquiry(ContactFormExtra_EnquiryModel &amp;$model) {
        // do stuff here
      }
  
  }
  </code></pre>
  
  <p><code>Subscriber</code> plugin:
  (<code>plugins/subscribe/services/SubscribeService.php</code>)</p>
  
  <pre><code>class SubscribeService extends BaseApplicationComponent
  {
    protected $settings;
  
    private function _getSettings()
    {
      $plugin_settings = craft()-&gt;plugins-&gt;getPlugin('subscribe')-&gt;getSettings();
      $this-&gt;settings = $plugin_settings;
    }
  
    public function subscribe($email)
    {
      $this-&gt;_getSettings();
      // do the subscribing here...
    }
  
  }
  </code></pre>
  
Tags: <plugin-development>
Title: Calling a method from a service in another plugin from an event
ThreadText: # Calling a method from a service in another plugin from an event
  
  (Asked by: username_0 on 2015-09-17)
  
  I'm having a problem calling a service method from another plugin when hooking into the `beforeSend` event in the [ContactForm](https://github.com/pixelandtonic/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...
    }
  
  }
  
  ```
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-09-17. Score: 1 | Accepted answer
  
  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. :)
  
  > **Comments:**
  > **username_0**: Thanks so much username_1 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.  
  
  ---
  
  ### Answer by username_2 on 2015-09-17. Score: 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.
  
  > **Comments:**
  > **username_0**: Thanks for the tips username_2 - 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](http://craftcms.stackexchange.com/questions/847/error-when-calling-methods-between-two-services-triggered-by-init)?  ie. the service in the `Subscribe` plugin isn't registered yet?  
  > **username_0**: 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.`  
  
  ---
  
  