Porting Events

Last updated on
30 January 2022

Tasks

  1. Make a list of events that your module generates. In Drupal 7, Rules events are defined by hook_rules_event_info(), which usually may be found in <modulename>/<modulename>.rules.inc (although, some modules implement this hook in <modulename>/<modulename>.module instead). Refer to the D7 version of this file to ensure that you port ALL the events that were in D7.
  2. Define a <modulename>/<modulename>.rules.events.yml for each module that generates events. Use rules/rules.rules.events.yml as an example of what should be in this file.
  3. Write a <modulename>/src/Event/<eventname>.php class for each event that module generates. Use the classes provided by the Rules module in rules/src/Event/ as examples.
  4. Search your module's Drupal 7 codebase for rules_invoke_event() calls, and replace these in Drupal 8 or 9 with something like:
      $event = new EventName($arguments);
      \Drupal::service('event_dispatcher')->dispatch($event, $event::EVENT_NAME);

    (Note that EventName should be replaced by the actual name of your event class, and $arguments should be replaced by the actual argument or arguments, separated by commas, that you need to pass to your event. Also, the event_dispatcher service should be injected where possible instead of using a static call to \Drupal::service().)

  5. Manually test, in the Rules UI, that each event shows up in the list of available events when you try to add a new reaction rule.
  6. Manually review code to make sure all comments and text strings are correct for each event.
  7. Manually test, by creating test reaction rules in the Rules UI, that each event triggers when expected. It's useful to use the "Show a message on the site" action in these test rules, which will immediately display message text on your website in response to the selected event.
  8. Write unit test cases for each event. Use the code in rules/tests/src/Unit/Integration/Event/* as examples.
  9. Write functional tests for each event. Use the code in rules/tests/src/Kernel/* as examples.

Try not to skip writing the tests - these are EXTREMELY useful for maintaining your module and for documenting how you expect your events to work.

Help improve this page

Page status: No known problems

You can: