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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

eojthebrave created an issue. See original summary.

eojthebrave’s picture

Assigned: Unassigned » eojthebrave
Torenware’s picture

The 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.

eojthebrave’s picture

I 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.

eojthebrave’s picture

Status: Active » Needs review
FileSize
19.04 KB

Okay. Here's a first pass at a hooks_example module.

Torenware’s picture

First 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.

  1. +++ b/hooks_example/hooks_example.module
    @@ -0,0 +1,226 @@
    + * Hooks are specially named functions called at critical points in order to
    

    Not sure if I can come up with a better word than "critical", but if you can think of one, worth it.

  2. +++ b/hooks_example/hooks_example.module
    @@ -0,0 +1,226 @@
    + * Every hook has three parts; A name, an implementation, and a definition.
    

    "a name"

  3. +++ b/hooks_example/hooks_example.module
    @@ -0,0 +1,226 @@
    + * When implementing a hook you can use the standard "Implements HOOK_NAME."
    

    "should use" -- it's a standard.

  4. +++ b/hooks_example/hooks_example.module
    @@ -0,0 +1,226 @@
    +  $session = \Drupal::request()->getSession();
    

    Alternatively, you could use the State API here. Not a problem, but would demonstrate a good practice.

Torenware’s picture

On second thought: yeah, use the session. Separating users in state would be a mess.

eojthebrave’s picture

FileSize
2.4 KB

Haha. 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.

eojthebrave’s picture

Hmm. Here's the patch for real.

eojthebrave’s picture

Assigned: eojthebrave » Unassigned

  • Torenware committed 96da25b on 8.x-1.x authored by eojthebrave
    Issue #2839588 by eojthebrave: Add hook_examples module to demonstrate...
Torenware’s picture

Much thanks, and a happy new year.

Torenware’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.