Problem/Motivation
Drupal 11.1 introduced support for implementing hooks as class methods using the #[Hook] PHP attribute. This is now the recommended approach for new and actively maintained modules targeting Drupal 11+, while still allowing backward compatibility with function-based hooks via the LegacyHook attribute where needed.
The Acquia DAM module currently implements hooks in acquia_dam.module as traditional global functions (for example, hooks related to media, routing, permissions, and configuration). To align with modern Drupal best practices and improve maintainability, testability, and discoverability of hooks, these should be converted to class-based implementations using #[Hook].
This modernization will also make it easier to inject services properly, group hook logic by concern (e.g., form hooks, entity hooks), and reduce procedural code in the .module file.
Proposed resolution
- Identify all hook implementations currently defined in acquia_dam.module (e.g. acquia_dam_help(), entity hooks, form alters, bundle create/delete hooks for media, etc.).
- Create one or more classes under the module’s src/Hook namespace, for example:
- Drupal\acquia_dam\Hook\AcquiaDamFormHooks
- Drupal\acquia_dam\Hook\AcquiaDamEntityHooks
- Drupal\acquia_dam\Hook\AcquiaDamSystemHooks
- For each existing hook implementation:
- Move the implementation logic into an appropriate class method.
- Add the #[Hook('short_hook_name')] attribute to that method, using the hook’s short name (without the hook_ prefix), as documented in the hooks API group.
- Where dependency injection is useful (e.g., logger, entity type manager, config factory), inject services into the hook class via the constructor instead of using \Drupal::service() calls.
- Keep the module compatible with Drupal 10/early 11 if required by:
- Either leaving the old function-based hooks in place and delegating to the new classes using LegacyHook where appropriate,
- Or using the recommended backward-compatible pattern from the Drupal 11 hook attribute change record (class-based hook plus legacy function wrapper if needed).
- Update any existing tests that reference the old procedural functions, if necessary, to ensure they still pass with the new class-based hooks.
- Ensure no functional regressions in:
- Media type creation/deletion hooks (e.g., DAM media bundles and field registration)
- Configuration form alters and validation (Acquia DAM settings at /admin/config/media/acquia_dam)
- User-level or site-level integration hooks, if present.
Issue fork acquia_dam-3563866
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
Comment #4
rajeshreeputraFailing tests will be looked post #3564450: Drop Drupal 9 support and release 1.2.0 (target Drupal 10+) and #3559554: Create service aliases for public services to improve autowiring support..