Drupal 8 introduced the concept of Events, alongside hooks, as a way for code components to communicate with one another.
There is currently no example code that demonstrates how to subscribe to an event, or how to dispatch an event. In addition to plugins, and hooks, which are both covered already, events are a common means for extending, and altering Drupal core. However, it seems like this would be a good thing for us to cover. Similar to how we've got plugins and hooks examples already.
I've been working on an example of this already for some new Drupalize.Me tutorials and will post a patch with my progress shortly.
Comment | File | Size | Author |
---|---|---|---|
#14 | interdiff.txt | 7.59 KB | eojthebrave |
#14 | 2841453-14-events_example.patch | 23.85 KB | eojthebrave |
| |||
#7 | interdiff.txt | 3.19 KB | eojthebrave |
#7 | 2841453-7-events_example.patch | 22.46 KB | eojthebrave |
| |||
#5 | 2841453-4-events_example.patch | 22.46 KB | eojthebrave |
|
Comments
Comment #2
eojthebraveHere's my in progress work on this. Right now the example creates a new form at examples/events-example that when submitted dispatches a new event. The new event is documented following examples from core, and dispatched using the event dispatcher service. The module also provides an event subscriber for the new event that demonstrates how to subscribe to an event.
This definitely needs a copy review for the comments, and a code review. I've run it through phpcs so it should be good as far as standards go.
This still needs tests. Probably a set of functional tests that submit the form and verify that the appropriate message is displayed. This will confirm the example can be accessed, and that the event is dispatched, and that the subscriber responds. There are two subscribers, with different messages, both should be tested.
Comment #3
eojthebraveThis new patch contains some tests to ensure things are working properly.
Comment #5
eojthebraveGuess it helps if the file isn't empty.
Comment #6
Amber Himes MatzCopy edit review only. (Code not reviewed.)
Use colon (:) instead of a semi-colon.
Change compound "you're" to possessive "your". "If your application..."
Seems like there's an extra unnecessary word. My suggestion: "Group related events together..." (Omit the word "like".)
Hyphenate "catch-all".
Add commas for phrasing: "This way, if, for example,"...
Add colon after "here:" and possibly put URL on a new line for readability and line-length.
Comment #7
eojthebraveThanks @Amber Himes Matz. Here's an updated patch.
Comment #8
Amber Himes MatzGreat! Also, just copy edited the issue title.
Example still needs a code review.
Comment #9
Mile23Thanks... Glad to see this example.
A few coding standards errors:
Some review-y stuff:
Let's add a kernel test that just grabs this service from the container, to verify that it's reachable.
Add a line between these.
Explain in the class docblock why this class is final.
Move this to the class docblock.
Add a blank line between these.
Comment #10
Mile23Some editorial review:
This is a dense paragraph... I'd say about half of it should be another paragraph explaining that modules can dispatch events but they don't need to, and can just subscribe to existing events.
Events don't actually allow modules to alter anything, or at least shouldn't. It's just a ping that something happened.
Who's subscribing? Also condense these to one line.
Who's dispatching? Also condense these to one line.
Yay drupalize.me. :-)
Docs say it should be static, but it's not.
I think @Event isn't really 'additionally' important. :-) It should come first in describing how this works.
Comment #11
joshi.rohit100No fear when batman is here. No need to call the police :) first thought that came in mind while reviewing it.
Comment #12
yogeshmpawarComment #13
yogeshmpawarComment #14
eojthebraveHere's an update that address all the comments from @Mile23's awesome review. Thanks. The only one I'm not 100% sure about is re-writing the previously dense intro paragraph. I tried to break it up into smaller, and more parseable bits. Let me know if this works for you.
Comment #16
eojthebraveTrying again. The patch seems fine. This looks like the testbot didn't have a copy of the examples project maybe?
Comment #17
Mile23Testbot issues aside, +1 on the changes and the patch does in fact apply. Thanks.
Let's see if we can kick the testbot into behaving...
Comment #19
Mile23OK, the testbot is happier now. ::unhappy glances at the DA::
Committed, pushed. :-)
Thanks!