Problem/Motivation

Drupal 11 introduces a new attribute-based hook system (#[Hook()]) that replaces the traditional procedural .module hook implementations. While procedural hooks still work via #[LegacyHook], they are deprecated and will be removed in a future major version.

Converting our hooks to OOP format ensures forward compatibility, aligns with Drupal core's direction, and brings the benefits of class-based architecture: better testability, autoloading, and clearer code organization.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mably created an issue. See original summary.

mably’s picture

Status: Active » Needs review

Converted all procedural hooks in publication_date.module to OOP format using #[Hook] attributes with #[LegacyHook] backward compatibility.

New file

src/Hook/PublicationDateHooks.php — OOP hook class with #[Hook] attributes for:

  • hook_help() -> help()
  • hook_entity_base_field_info() -> entityBaseFieldInfo()
  • hook_form_node_form_alter() -> formNodeFormAlter()
  • hook_clone_node_alter() -> cloneNodeAlter()
  • hook_field_formatter_info_alter() -> fieldFormatterInfoAlter()

The formNodeFormAlter() method injects AccountProxyInterface via constructor instead of calling \Drupal::currentUser() directly.

Updated files

publication_date.module — All procedural hooks now carry #[LegacyHook] and delegate to \Drupal::service(PublicationDateHooks::class).

publication_date.services.yml — Added the hooks class entry with autowire: true for the AccountProxyInterface dependency.

Not converted

publication_date.install (hook_install is in the OOP deny list) and publication_date.post_update.php (post_update hooks are not convertible) were left as-is.

Tests

All 13 existing tests pass (155 assertions).

  • mably committed d36c5e9b on 3.x
    task: #3574810 Convert all hooks to OOP format
    
    By: mably
    
mably’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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