Objective

  1. #1892574: Remove hook_hook_info_alter() was removed a long time ago already.

  2. hook_hook_info() only exists for lazy-loading legacy hook implementation code.

  3. hook_hook_info() has architectural problems causing code to not be loaded (see related issues).

  4. ModuleHandler apparently maintains two cache items; one for 'module_implements', and another one just for 'hook_info'.

  5. Object-oriented code can be properly auto-loaded in D8.

Remaining implementations

  1. system_hook_info() (hook_token*())
  2. views_hook_info() (hook_views_*())

Proposed solution

  1. Remove hook_hook_info() + persistent caching from ModuleHandler.

  2. Convert remaining affected hook implementations...

    A) Either move them into .module files.

    B) Or find a simple object-oriented replacement pattern.

Comments

sun’s picture

Issue summary: View changes

Clarifying summary that ModuleHandler maintains a separate cache item for hook_info.

sun’s picture

tim.plunkett’s picture

Issue tags: +VDC

lazy-loading legacy hook implementation code

Please have a look at views_hook_info().

None of that is legacy. Until something like #1972304: Add a HookEvent or #2043653: Allow modules to implement hooks by nominating methods in a module.implements.yml goes in, I don't see how or why we should move forward here.

catch’s picture

#1443308-97: Add static cache to module_load_include() shows that views_hook_info() is a net negative for performance. With an opcode cache, having the hook implementations in the module file has a negligible impact, vs. the discovery which is non-negligible.

catch’s picture

Title: Remove hook_hook_info() + corresponding hook_info persistent cache » Deprecate hook_hook_info()
Version: 8.0.x-dev » 8.1.x-dev

Also we can't nuke this until 9.x unfortunately, but we can discourage it before then.

David_Rothstein’s picture

I like the idea of killing it, although it would be nice for some of the code organization benefits that it provides to be retained (and in fact expanded).

But I guess that can still be done with require_once statements at the top of the .module file...

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dpi’s picture

Version: 8.3.x-dev » 9.x-dev

Should we issue a deprecation notice?

deprecated in Drupal 8.0.x, will be removed in Drupal 9.0.x

joelpittet’s picture

Version: 9.x-dev » 8.3.x-dev

@dpi That sounds good but 8.3.x or 8.4.x would be the start were it was deprecated.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Mile23’s picture

We're trying to figure out how to @trigger_error() for deprecated hooks here: #2870058: [policy, no patch] Document how to deprecate hooks

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

donquixote’s picture

I don't understand why this is being suggested.

If we replace hooks completely, fine. But we are not there yet I think.
Until then, something like hook_hook_info() is useful for code organization, similar to autoloading for classes (not autoloading itself, but the PSR-4 file organization that is made necessary by autoloading).

views_hook_info() is a net negative for performance. With an opcode cache, having the hook implementations in the module file has a negligible impact, vs. the discovery which is non-negligible.

To me, performance is not the main reason for not putting everything into the main *.module file.

I like the idea of killing it, although it would be nice for some of the code organization benefits that it provides to be retained (and in fact expanded).

Yes. Code organization +1.

But I guess that can still be done with require_once statements at the top of the .module file...

Yes, but:

  • no longer forces a convention. Instead, you can now put your hook implementations into whichever file you want. Conventions are good. Forced conventions are good.
  • requires explicit require_once statements, whereas with a hook_hook_info() you can simply create the file with the hook implementation. Such lists of require_once are a typical source of merge conflicts - easily resolved, but still annoying.

The issue summary contains some more arguments, which are quite old.
I don't know if these are still valid.

As long as we have hooks, it would be useful to be able to force a convention for file organization per hook.
I am not married to hook_hook_info() specifically, but we should find more convincing reasons to remove it, and/or suggest a good replacement.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Removed field_hook_info() from IS as it was moved to views_hook_info() in #2399931: Generic entity api field handler should live in views module not in field module

So only tokens and views using this hook but it needs to deprecate \Drupal\Core\Extension\ModuleHandlerInterface::getHookInfo() as well

Filed follow-up to remove broken ref #3346614: Remove stray reference to field_hook_info()

catch’s picture

If we replace hooks completely, fine. But we are not there yet I think.
Until then, something like hook_hook_info() is useful for code organization, similar to autoloading for classes (not autoloading itself, but the PSR-4 file organization that is made necessary by autoloading).

Just to answer #19 - a lot of modules and some of core too already puts the logic of their hook implementations into classes, so that the hook implementation in .module is just three lines or so. This pattern is available for every hook, not just the ones that have hook_hook_info() implemented for them.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.