On this page
Porting Events
Last updated on
30 January 2022
Tasks
- 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>.moduleinstead). Refer to the D7 version of this file to ensure that you port ALL the events that were in D7. - Define a
<modulename>/<modulename>.rules.events.ymlfor each module that generates events. Userules/rules.rules.events.ymlas an example of what should be in this file. - Write a
<modulename>/src/Event/<eventname>.phpclass for each event that module generates. Use the classes provided by the Rules module inrules/src/Event/as examples. - 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().)
- 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.
- Manually review code to make sure all comments and text strings are correct for each event.
- 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.
- Write unit test cases for each event. Use the code in
rules/tests/src/Unit/Integration/Event/*as examples. - 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:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion