Problem/Motivation

Drupal core has a deprecation process documented here: https://www.drupal.org/core/deprecation

What's missing is a standard way to deprecate hooks.

The following hooks are already marked as deprecated: hook_rest_type_uri_alter(), hook_rest_relation_uri_alter()

Proposed resolution

The deprecation process:

  • Mark the hook as @deprecated in the *.api.php file that documents it.
  • Convert the invocation of the hook to use ModuleHandlerInterface::deprecatedInvoke() or its siblings instead of invoke(). Alter hooks would call deprecatedAlter(). These methods on ModuleHandler will then call @trigger_error() if the installation has any implementations of that hook. Implementation issue for this strategy is here: #2866779: Add a way to trigger_error() for deprecated hooks
  • Add change records for deprecated hooks explaining how to accomplish use-cases without the hook.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Mile23 created an issue. See original summary.

mile23’s picture

Issue summary: View changes
alexpott’s picture

Convert the invocation of the hook to call @trigger_error() as with other deprecated code, since most IDEs won't be able to see that hooks are deprecated.

I was thinking this might be one way of doing it - but an issue is that other places can invoke the hook too - I was wondering if the module handler shouldn't get a list of deprecated hooks and trigger an error if one is used.

mile23’s picture

#2866779: Add a way to trigger_error() for deprecated hooks adds methods to ModuleHandlerInterface like invokeDeprecated().

So you find places where the hooks are invoked and switch them to invokeDeprecated() which signals with a @trigger_error() if there are any implementations.

That issue is currently postponed because there's not yet a good way to test whether it works. :-)

dawehner’s picture

We could also introduce 'deprecated' on hook_hook_info). Personally I don't like the idea of hook_hook_info() but at least this here seem to be a good usage.

joachim’s picture

> but an issue is that other places can invoke the hook too

I would say that if you're invoking a hook that is not invented by your own module, you're doing it wrong.

Hooks fall into these groups:

- alter hooks, which change a data structure managed by a module. You should be using the API that gets the overall data
- reaction hooks. You should be calling the API that causes the action, eg entity_save()
- declaration hooks (we still have a few of these it seems!) Same as alter hooks, you should be using the API that gathers the things being declared.

alexpott’s picture

@dawehner hook_hook_info() seems like a really good idea.
@joachim I agree it is a bad idea but that has not stopped people from doing it.

mile23’s picture

alexpott’s picture

@Mile23 at that point Drupal no longer has hooks so we're good to go :)

joachim’s picture

> @joachim I agree it is a bad idea but that has not stopped people from doing it.

True, but are we then responsible for notifying these people in their code? And also, if they don't care about working around APIs like this, then how likely are they to care about not using deprecated APIs?

alexpott’s picture

@joachim they're not likely to care at all - but maybe people who use a contrib module that does this will appreciate that they can discover this module is using a deprecated hook.

mile23’s picture

@Mile23 at that point Drupal no longer has hooks so we're good to go :)

I hear you, but we have to deprecate it before it's gone. :-)

dawehner’s picture

I hear you, but we have to deprecate it before it's gone. :-)

Guess what, we adapt the ModuleHandler and call it a day.

catch’s picture

hook_hook_info() is optional. It was added to put hooks into a separate file, not to document the hooks. Even if we're not ready to deprecate it yet (I think Views is the big info hook user mostly remaining), we shouldn't encourage people to use it either.

I think I prefer adding the methods though to be honest. Then you just delete the invocations and the hook_*() documentation and you're done when you're ready to drop bc.

mile23’s picture

Issue summary: View changes
Status: Active » Needs review

So shall we move forward with the implementation? I think as @catch points out hook_hook_info() is optional, and the deprecatedInvoke*() methods are a more consistent conversion during the deprecation process.

dawehner’s picture

I agree, its maybe a bit more explicit what is going on.

cilefen’s picture

Issue summary: View changes
mile23’s picture

mile23’s picture

Added a change notice: https://www.drupal.org/node/2881531

It'll need updating after #2866779: Add a way to trigger_error() for deprecated hooks is finalized, tagging and work occurring there.

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.

larowlan’s picture

Title: Figure out how to deprecate hooks » [policy, no patch] Document how to deprecate hooks
Status: Needs review » Reviewed & tested by the community

This looks good, once its in we'll need to update the deprecation policy

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Docs were updated, adding issue credits and marking fixed

Final documentation https://www.drupal.org/core/deprecation#how-hook

Status: Fixed » Closed (fixed)

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