Answers.Body: <p>Not really – there's no priority system built into events for Craft or Yii.</p>
  <p><s>The easy way out is taking advantage of the fact that Craft actually loads plugins alphabetically, by name (i.e. the string returned by the plugins' <code>getName()</code> method). This means that if your plugins are in fact named &quot;Plugin A&quot; and &quot;Plugin B&quot;, then Plugin A will be loaded before Plugin B and you can be sure that its event listener is also attached earlier, meaning that – in theory, I haven't tested this – it's event listener should also be called before Plugin B's event listener.</s>
  <strong>Edit: This doesn't actually appear to work, at least not reliably.</strong></p>
  <p>Of course, the correct way to solve the issue is to write your code in a way that negates the need for sequentially dependent event handlers. One way to do this is to use <a href="https://craftcms.stackexchange.com/questions/8886/can-a-plugin-fire-a-custom-event?rq=1">custom events</a> – for instance, you could have Plugin A listen to the native <code>entries.saveEntry</code> event, but have Plugin B listen to some custom event instead, where Plugin A fires the custom event after completing its work inside the <code>saveEntry</code> handler.</p>
  <p>You could also consider dropping the event listener altogether for Plugin B, and just call one of its service methods directly from Plugin A's event listener (you can even pass in the event object if you need it):</p>
  <pre><code>// Plugin A
  craft()-&gt;on('entries.saveEntry', function (Event $event)
  {
      ...
      // Stuff happens, then let Plugin B do its thing
      $pluginB = craft()-&gt;plugins-&gt;getPlugin('pluginB');
      if ($pluginB) {
          craft()-&gt;pluginB-&gt;someMethodName($event);
      }
  }
  </code></pre>
  <p>...and in Craft 3:</p>
  <pre><code>// Plugin A
  yii\base\Event::on(
      craft\elements\Entry::class,
      craft\base\element::EVENT_AFTER_SAVE,
      function (craft\events\ModelEvent $event) {
          ...
          // Stuff happens, then let Plugin B do its thing
          $pluginB = Craft::$app-&gt;getPlugins()-&gt;getPlugin('plugin-b');
          if ($pluginB) {
              $pluginB-&gt;someService-&gt;someMethodName($event);
          }
      }
  );
  </code></pre>
  <p>Beyond this, its hard to advise without seeing some code examples or a more elaborate use case.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-20T10:12:08.797Z
Answers.Text: Unfortunately relying on alphabetical order did not work for me. I ended up with calling Plugin B directly from Plugin A
Answers.UserDisplayName: Yuri Salimovskiy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-20T10:15:06.260Z
Answers.Text: Allright, thanks for clearing that up @YuriSalimovskiy :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-18T02:23:12.657Z
Answers.Text: @MatsMikkelRummelhoff how can we do the same for Craft 3?
Answers.UserDisplayName: Bhashkar Yadav
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-19T06:22:03.580Z
Answers.Text: @BhashkarYadav Just updated the answer with that same code example for Craft 3.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2016-06-19T18:47:37.830Z
Answers.LastActivityDate: 2021-10-19T06:21:43.720Z
Answers.LastEditDate: 2021-10-19T06:21:43.720Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I have 2 separate plugins that listen to same event.</p>
  
  <p>Now I want to make sure Plugin B starts its action only after Plugin A has finished its job.</p>
  
  <p>Is there a way in Craft CMS to set priority / execution order for plugins that listen to same event?</p>
  
Tags: <plugin-development><events>
Title: Set execution order of plugins listening to same event
ThreadText: # Set execution order of plugins listening to same event
  
  (Asked by: username_0 on 2016-06-19)
  
  I have 2 separate plugins that listen to same event.
  
  Now I want to make sure Plugin B starts its action only after Plugin A has finished its job.
  
  Is there a way in Craft CMS to set priority / execution order for plugins that listen to same event?
  
  **Tags:** plugin-development, events
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-06-19. Score: 5 | Accepted answer
  
  Not really – there's no priority system built into events for Craft or Yii.
  
  ~~The easy way out is taking advantage of the fact that Craft actually loads plugins alphabetically, by name (i.e. the string returned by the plugins' `getName()` method). This means that if your plugins are in fact named "Plugin A" and "Plugin B", then Plugin A will be loaded before Plugin B and you can be sure that its event listener is also attached earlier, meaning that – in theory, I haven't tested this – it's event listener should also be called before Plugin B's event listener.~~
  **Edit: This doesn't actually appear to work, at least not reliably.**
  
  Of course, the correct way to solve the issue is to write your code in a way that negates the need for sequentially dependent event handlers. One way to do this is to use [custom events](https://craftcms.stackexchange.com/questions/8886/can-a-plugin-fire-a-custom-event?rq=1) – for instance, you could have Plugin A listen to the native `entries.saveEntry` event, but have Plugin B listen to some custom event instead, where Plugin A fires the custom event after completing its work inside the `saveEntry` handler.
  
  You could also consider dropping the event listener altogether for Plugin B, and just call one of its service methods directly from Plugin A's event listener (you can even pass in the event object if you need it):
  
  ```
  // Plugin A
  craft()->on('entries.saveEntry', function (Event $event)
  {
      ...
      // Stuff happens, then let Plugin B do its thing
      $pluginB = craft()->plugins->getPlugin('pluginB');
      if ($pluginB) {
          craft()->pluginB->someMethodName($event);
      }
  }
  
  ```
  
  ...and in Craft 3:
  
  ```
  // Plugin A
  yii\base\Event::on(
      craft\elements\Entry::class,
      craft\base\element::EVENT_AFTER_SAVE,
      function (craft\events\ModelEvent $event) {
          ...
          // Stuff happens, then let Plugin B do its thing
          $pluginB = Craft::$app->getPlugins()->getPlugin('plugin-b');
          if ($pluginB) {
              $pluginB->someService->someMethodName($event);
          }
      }
  );
  
  ```
  
  Beyond this, its hard to advise without seeing some code examples or a more elaborate use case.
  
  > **Comments:**
  > **username_0**: Unfortunately relying on alphabetical order did not work for me. I ended up with calling Plugin B directly from Plugin A  
  > **username_1**: Allright, thanks for clearing that up @username_0 :)  
  > **username_2**: @username_1 how can we do the same for Craft 3?  
  > **username_1**: @username_2 Just updated the answer with that same code example for Craft 3.  
  
  ---
  
  