I'm working on some example code for use in some Drupalize.Me documentation/tutorials right now, and I'm wondering if it might be useful to add it to the examples project. The module is intended to explain the use-case for hooks, and then demonstrate how to implement a hook. As well as how to define, document, and invoke a new hook. Both are important skills for a module developer to have. And I think this project could benefit from having some solid code examples of doing so to point at.
We've already got examples for implementing plugins, and defining new plugin types. and I could see it being useful to add one for events in addition to hooks and plugins.
Assigning to myself to work on for now. Any feedback on wether or not this is something that should be included would be appreciated.
Comment | File | Size | Author |
---|---|---|---|
#9 | 2839588-8-hooks_example.patch | 19.03 KB | eojthebrave |
| |||
#8 | interdiff.txt | 2.4 KB | eojthebrave |
#5 | 2839588-5-hooks_example.patch | 19.04 KB | eojthebrave |
|
Comments
Comment #2
eojthebraveComment #3
Torenware CreditAttribution: Torenware as a volunteer and commentedThe one thing I might recommend adding would be an event-based example (i.e., implement an event and a listener), ideally for the same example you do the hook for. We're moving slowly away from hooks now that we can do listeners easily, and having the example show how to do the same job using both mechanisms is a good way to make people aware this exists as an option.
Comment #4
eojthebraveI like the idea of having an event-based example too. Though, I'm not sure that it would be doing the exact same things as the hooks example. As far as I'm aware there's either an event, or a hook, to accomplish a task but not both.
Comment #5
eojthebraveOkay. Here's a first pass at a hooks_example module.
Comment #6
Torenware CreditAttribution: Torenware as a volunteer and commentedFirst of all, for a first draft, this is really good. Well worded, and if you didn't already run this through phpcs, you're the most careful coder I've ever seen :-)
I have a few minor quibbles, some of which you should feel free to ignore. But in general: this code uses our best practices, and has a meaningful *phpunit* based test. Not much that you need to improve here.
Not sure if I can come up with a better word than "critical", but if you can think of one, worth it.
"a name"
"should use" -- it's a standard.
Alternatively, you could use the State API here. Not a problem, but would demonstrate a good practice.
Comment #7
Torenware CreditAttribution: Torenware as a volunteer and commentedOn second thought: yeah, use the session. Separating users in state would be a mess.
Comment #8
eojthebraveHaha. I totally ran phpcs already. :) I wish I could claim to be that meticulous, but really I'm just doing what the machines tell me to do.
This patch contains updates per your review. Thanks for taking a look.
Comment #9
eojthebraveHmm. Here's the patch for real.
Comment #10
eojthebraveComment #12
Torenware CreditAttribution: Torenware as a volunteer and commentedMuch thanks, and a happy new year.
Comment #13
Torenware CreditAttribution: Torenware as a volunteer and commented