drupal-code-generator icon indicating copy to clipboard operation
drupal-code-generator copied to clipboard

Add object oriented hook implementations

Open rodrigoaguilera opened this issue 1 year ago • 10 comments

Drupal >11.1.0 has support for hook implementations in classes with attributes. https://www.drupal.org/node/3442349

And it seems that many core hooks are moving to this kind of implementation.

Is there any interest in implementing this early to generate code?

rodrigoaguilera avatar Nov 19 '24 21:11 rodrigoaguilera

Sounds like good idea

andypost avatar Nov 19 '24 23:11 andypost

I propose generating single hook classes like following.

#[Hook('help')]
final readonly class Help {

  /**
   * Implements hook_help().
   */
  public function __invoke(): void {
  
  }

}

Chi-teck avatar Dec 20 '24 04:12 Chi-teck

Nice ... I propose omitting the Implements hook_help(). doxygen. I know that core kept it so far but thats because they couldn't quickly agree on how to deal with api module integration. This isn't a concern for custom drupal code. I think Drupal core will eventually get rid of this duplicative code comment.

weitzman avatar Dec 20 '24 09:12 weitzman

The relevant core issue https://www.drupal.org/project/drupal/issues/3493453

Chi-teck avatar Dec 20 '24 09:12 Chi-teck

Some static code analysis tools might whine a bit about a function with no doc comment though.

PierrePaul avatar Jan 02 '25 15:01 PierrePaul

Happy to see this being worked on

I like the single class, but for a lot of hooks it makes sense to group them, I think that would massively complicate this feature though and it's pretty easy to reorganize, so a single hook class with __invoke probably makes the most sense.

Not a fan of final on everything that gets generated, it just complicates things.

Also probably overkill here, but extended hook classes for things like #[FormAlter('form_id')] are likely coming soon too: https://www.drupal.org/project/drupal/issues/3479141

nlighteneddesign avatar Jan 22 '25 04:01 nlighteneddesign

Some static code analysis tools might whine a bit about a function with no doc comment though.

Thats a problem with the config of those analyzers. IMO should not add useless comments to satisfy the bots.

weitzman avatar Jan 23 '25 03:01 weitzman

To clarify the issue is not about static analyzers but code sniffers, namely drupal coder. There are a few issues on drupal.org about removing redundant dockblocks.

Chi-teck avatar Jan 23 '25 04:01 Chi-teck

FYI the organisation/naming/grouping strategy for Hook classes is being discussed here https://www.drupal.org/project/drupal/issues/3493453

IMO if drush is generating 1 class per hook that's fine, but I would lean heavily on the side of wanting a module name prefix in the class. Having 20 classes called "Help.php" is going to make for tiresome DX.

acbramley avatar Mar 18 '25 05:03 acbramley

What do the esteemed contributors prefer? Write the hook generator now with perhaps nonoptimal naming and fix later or punt?

chx avatar Apr 19 '25 17:04 chx