Answers.Body: <p>This will not work as the field value will be the new value in both events. You'll need to get the old value from the entry that exists in Craft before saving the new entry, and then perform the comparison. </p>
  
  <p>If the value has changed then store the entry ID to process once the entry has successfully saved. </p>
  
  <p>Note that the <code>$entryIdChanged</code> variable in the anonymous event functions is not in the same as that in the <code>init()</code> method. You can resolve this as follows:</p>
  
  <ol>
  <li>Bring the <code>$entryIdChanged</code> variable into the anonymous functions using the <code>use</code> operator.  </li>
  <li>PHP will by default "pass by value", meaning that if the value of the variable within the function is changed, it does not get changed outside of the function. So you should pass the variable in "by reference", using an ampersand <code>&amp;</code> before the variable name, which will change it outside the function as well.</li>
  </ol>
  
  <p>See the PHP guide on <a href="http://php.net/manual/en/language.references.pass.php" rel="nofollow noreferrer">Passing by Reference</a>.  </p>
  
  <pre><code>public function init()
  {
      parent::init();
  
      $entryIdChanged = null;
  
      // Event: onBeforeSaveEntry
      craft()-&gt;on('entries.beforeSaveEntry', function (Event $event) use (&amp;$entryIdChanged)
      {
          $entryModel = $event-&gt;params['entry'];
          $newVar     = $entryModel-&gt;fieldVar;
  
          $oldEntry = craft()-&gt;entries-&gt;getEntryById($entryModel-&gt;id);
          $oldVar = $oldEntry !== null ? $oldEntry-&gt;fieldVar : '';
  
          if ($oldVar !== $newVar){
              $entryIdChanged = $entryModel-&gt;id;
          }
      });
  
      // Event: onSaveEntry
      craft()-&gt;on('entries.SaveEntry', function (Event $event) use ($entryIdChanged)
      {
          $entryModel = $event-&gt;params['entry'];
  
          if ($entryModel-&gt;id === $entryIdChanged){
              // ...
          }
      });
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-12T21:09:58.843Z
Answers.Text: I'll try it. Actually I've recently been reading that PHP guide but I haven't had a chance to try it. But definitely I would start with `use ($oldVar)` without an ampersand `&`.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-13T07:27:46.437Z
Answers.Text: I've updated my answer to explain why the ampersand is important.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-20T11:04:14.920Z
Answers.Text: I'm testing it on _lightswitch field_ but I'm getting the same output value from _both events_. I've also checked what output value of that _lightswitch field_ will give `beforeSaveEntry` event. The output is always **oposite**.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-20T15:44:46.173Z
Answers.Text: I've rewritten the answer which should work or at least get you 90% of the way there :)
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-20T19:01:07.160Z
Answers.Text: That worked well :) But I'm thinking to pass the `$oldVar` to function on `saveEntry` event and I'll be sure that an email sends only if entry is saved. I've already tested and it works.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-20T19:32:10.873Z
Answers.Text: Check the new code.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-20T19:50:03.307Z
Answers.Text: I was just looking for the way to execute a statement only if entry changes. You have got me on the way again! I have more variables to compare so I'm thinking to assign an array of _old variables_ and compare it with an  array of _new variables_. I think  I should get there.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T08:25:40.607Z
Answers.Text: Yes that shold be relatively easy to do at this stage :)
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T08:34:30.550Z
Answers.Text: And I also wanted to point it out about your explanation earlier in the answer that you erased on How code works to put that back. Well, shouldn't have to. Thanks Ben!
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T16:16:51.617Z
Answers.Text: Any idea why _lightswitch_ field type after assigning to `$oldVar` gives _string type value_ and not _boolean type value_? Yes, I'm declaring `$oldVar` before functions but not as _string_ type but as _null_ type and than bringing it to the function.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-21T19:24:56.060Z
Answers.Text: I found out that _lightswitch_ field type has `boolean` value when is ON and `string` value when is OFF. What I did was converted outcome of both variables `$oldVar` & `$newVar` to `boolean` type value. Now I'm sure that both _arrays_ are not only equal but also identical.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-22T16:48:22.933Z
Answers.Text: I haven't noticed that you didn't referenced to `$entryIdChanged` in second _event listener_ by using ampersand `&`. Whole time I "accidentally" had `&` before `$entryIdChanged` and it worked. Logically it make sense no to pass `&` further but from curiosity I took `&` OFF and it stopped working. I did a test and `$oldVar` was of `NULL` type with  _empty value_ passed. Do you know why it needs to be referenced again?
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-22T20:07:53.997Z
Answers.Text: I don't see why it would have to, unless you are changing it.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-22T20:13:26.127Z
Answers.Text: The code structure is the same as in answer.
Answers.UserDisplayName: Dominik Krulak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-12T20:48:17.377Z
Answers.LastActivityDate: 2018-11-21T08:25:11.153Z
Answers.LastEditDate: 2018-11-21T08:25:11.153Z
Answers.UserDisplayName: Ben Croker
Body: <p>I need to find out if any changes have been made to the entry on successful save.</p>
  
  <p>Using events <a href="https://docs.craftcms.com/v2/plugins/events-reference.html#entries-onbeforesaveentry" rel="nofollow noreferrer">entries.onBeforeSaveEntry</a> and <a href="https://docs.craftcms.com/v2/plugins/events-reference.html#entries-onsaveentry" rel="nofollow noreferrer">entries.onSaveEntry</a> seems to be a way to do it. Maybe.</p>
  
  <p>Can I pass some entry variables as arguments to the function on event listener?</p>
  
  <pre><code>public function init()
  {
      parent::init();
  
      $oldVar = "";
  
      //Event: onBeforeSaveEntry
      craft()-&gt;on('entries.beforeSaveEntry', function (Event $event)
      {
          $entryModel = $event-&gt;params['entry'];
          $oldVar     = $entryModel-&gt;fieldVar;
      });
  
      //Event: onSaveEntry
      craft()-&gt;on('entries.saveEntry', function (Event $event)
      {
          $entryModel = $event-&gt;params['entry'];
          $newVar     = $entryModel-&gt;fieldVar;
  
          if ( $oldVar !== $newVar ){
              // ...
          }
      });
  }
  </code></pre>
  
Tags: <plugin-development><craft2>
Title: Is it possible to pass a variable to an event in a plugin?
ThreadText: # Is it possible to pass a variable to an event in a plugin?
  
  (Asked by: username_0 on 2018-11-10)
  
  I need to find out if any changes have been made to the entry on successful save.
  
  Using events [entries.onBeforeSaveEntry](https://docs.craftcms.com/v2/plugins/events-reference.html#entries-onbeforesaveentry) and [entries.onSaveEntry](https://docs.craftcms.com/v2/plugins/events-reference.html#entries-onsaveentry) seems to be a way to do it. Maybe.
  
  Can I pass some entry variables as arguments to the function on event listener?
  
  ```
  public function init()
  {
      parent::init();
  
      $oldVar = "";
  
      //Event: onBeforeSaveEntry
      craft()->on('entries.beforeSaveEntry', function (Event $event)
      {
          $entryModel = $event->params['entry'];
          $oldVar     = $entryModel->fieldVar;
      });
  
      //Event: onSaveEntry
      craft()->on('entries.saveEntry', function (Event $event)
      {
          $entryModel = $event->params['entry'];
          $newVar     = $entryModel->fieldVar;
  
          if ( $oldVar !== $newVar ){
              // ...
          }
      });
  }
  
  ```
  
  **Tags:** plugin-development, craft2
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-11-12. Score: 2 | Accepted answer
  
  This will not work as the field value will be the new value in both events. You'll need to get the old value from the entry that exists in Craft before saving the new entry, and then perform the comparison.
  
  If the value has changed then store the entry ID to process once the entry has successfully saved.
  
  Note that the `$entryIdChanged` variable in the anonymous event functions is not in the same as that in the `init()` method. You can resolve this as follows:
  
  1. Bring the `$entryIdChanged` variable into the anonymous functions using the `use` operator.
  2. PHP will by default "pass by value", meaning that if the value of the variable within the function is changed, it does not get changed outside of the function. So you should pass the variable in "by reference", using an ampersand `&` before the variable name, which will change it outside the function as well.
  
  See the PHP guide on [Passing by Reference](http://php.net/manual/en/language.references.pass.php).
  
  ```
  public function init()
  {
      parent::init();
  
      $entryIdChanged = null;
  
      // Event: onBeforeSaveEntry
      craft()->on('entries.beforeSaveEntry', function (Event $event) use (&$entryIdChanged)
      {
          $entryModel = $event->params['entry'];
          $newVar     = $entryModel->fieldVar;
  
          $oldEntry = craft()->entries->getEntryById($entryModel->id);
          $oldVar = $oldEntry !== null ? $oldEntry->fieldVar : '';
  
          if ($oldVar !== $newVar){
              $entryIdChanged = $entryModel->id;
          }
      });
  
      // Event: onSaveEntry
      craft()->on('entries.SaveEntry', function (Event $event) use ($entryIdChanged)
      {
          $entryModel = $event->params['entry'];
  
          if ($entryModel->id === $entryIdChanged){
              // ...
          }
      });
  }
  
  ```
  
  > **Comments:**
  > **username_0**: I'll try it. Actually I've recently been reading that PHP guide but I haven't had a chance to try it. But definitely I would start with `use ($oldVar)` without an ampersand `&`.  
  > **username_1**: I've updated my answer to explain why the ampersand is important.  
  > **username_0**: I'm testing it on _lightswitch field_ but I'm getting the same output value from _both events_. I've also checked what output value of that _lightswitch field_ will give `beforeSaveEntry` event. The output is always **oposite**.  
  > **username_1**: I've rewritten the answer which should work or at least get you 90% of the way there :)  
  > **username_0**: That worked well :) But I'm thinking to pass the `$oldVar` to function on `saveEntry` event and I'll be sure that an email sends only if entry is saved. I've already tested and it works.  
  > **username_1**: Check the new code.  
  > **username_0**: I was just looking for the way to execute a statement only if entry changes. You have got me on the way again! I have more variables to compare so I'm thinking to assign an array of _old variables_ and compare it with an  array of _new variables_. I think  I should get there.  
  > **username_1**: Yes that shold be relatively easy to do at this stage :)  
  > **username_0**: And I also wanted to point it out about your explanation earlier in the answer that you erased on How code works to put that back. Well, shouldn't have to. Thanks Ben!  
  > **username_0**: Any idea why _lightswitch_ field type after assigning to `$oldVar` gives _string type value_ and not _boolean type value_? Yes, I'm declaring `$oldVar` before functions but not as _string_ type but as _null_ type and than bringing it to the function.  
  > **username_0**: I found out that _lightswitch_ field type has `boolean` value when is ON and `string` value when is OFF. What I did was converted outcome of both variables `$oldVar` & `$newVar` to `boolean` type value. Now I'm sure that both _arrays_ are not only equal but also identical.  
  > **username_0**: I haven't noticed that you didn't referenced to `$entryIdChanged` in second _event listener_ by using ampersand `&`. Whole time I "accidentally" had `&` before `$entryIdChanged` and it worked. Logically it make sense no to pass `&` further but from curiosity I took `&` OFF and it stopped working. I did a test and `$oldVar` was of `NULL` type with  _empty value_ passed. Do you know why it needs to be referenced again?  
  > **username_1**: I don't see why it would have to, unless you are changing it.  
  > **username_0**: The code structure is the same as in answer.  
  
  ---
  
  