Problem/Motivation
Patternkit has two procedural hook functions that generate PHP deprecation notices on Drupal 11.4: patternkit_rebuild() in patternkit.module and patternkit_media_library_element_info_alter() in patternkit_media_library.module.
Drupal 11.2 introduced the #[LegacyHook] attribute to bridge procedural implementations to OOP #[Hook] dispatch. Drupal 11.4 deprecated procedural hooks that do not use this bridge. All other procedural hooks in Patternkit already carry #[LegacyHook] and delegate to Hook classes with #[Hook] attributes. These two were missed.
On Drupal 11.4, any site with Patternkit installed will see deprecation notices originating from these two functions on every cache clear and request that processes element info.
Steps to reproduce
- Install Patternkit on a Drupal 11.4 site.
- Enable
patternkit_media_library. - Clear caches (
drush cr). - Observe PHP deprecation notices for
patternkit_rebuild()andpatternkit_media_library_element_info_alter().
Proposed resolution
Migrate both functions to OOP #[Hook]-attributed methods and remove the procedural wrappers.
patternkit_rebuild()
Add a rebuild() method to PatternkitHooks with #[Hook('rebuild')]. The method injects patternkit.library.namespace_resolver and patternkit.pattern.discovery as constructor dependencies and calls ->rebuild() on each. Remove patternkit_rebuild() from patternkit.module.
Drupal core's BlockHooks::rebuild() in block/src/Hook/BlockHooks.php provides a direct precedent for this pattern.
patternkit_media_library_element_info_alter()
Add an elementInfoAlter() method to a new PatternkitMediaLibraryHooks class in modules/patternkit_media_library/src/Hook/, decorated with #[Hook('element_info_alter')]. The method body appends JsonEditorFormElementAlterHandler::class to the #pre_render array for jsoneditor_form elements. No service injection is required. Register the class in patternkit_media_library.services.yml or confirm auto-discovery. Remove patternkit_media_library_element_info_alter() from patternkit_media_library.module.
Remaining tasks
- Review and test the merge request.
- CI validation.
User interface changes
None.
Introduced terminology
None.
API changes
None.
Data model changes
None.
Release notes snippet
Patternkit now uses OOP #[Hook]-attributed methods for hook_rebuild and hook_element_info_alter, removing the last two procedural hook functions that generated PHP deprecation notices on Drupal 11.4. Sites running Drupal 11.4 with Patternkit and patternkit_media_library enabled will no longer see these deprecation notices after upgrading.
Issue fork patternkit-3608056
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 #2
nakaza sora commentedComment #4
nakaza sora commentedMigrated both procedural hooks to OOP #[Hook] methods and removed the procedural functions: hook_rebuild now lives in PatternkitHooks::rebuild() (injecting the namespace resolver and pattern discovery services, following BlockHooks::rebuild()), and hook_element_info_alter in a new PatternkitMediaLibraryHooks class.
Confirmed the hook classes are auto-discovered, so no services.yml registration is needed.
Please review.
Comment #5
sluceroThanks for the patch! The core hook migration was solid. During CI review I found three issues and added follow-up commits to address them. These were all gaps stemming from the the implementation plan the way I'd written it.
If you get a chance to look over the follow-up changes and let me know if you have any questions or follow-ups, we should be able to get this merged in.
Service aliases for PatternkitHooks constructor
When
HookCollectorPassregisters hook classes it marks them for auto-wiring. The two new constructor params onPatternkitHookstype-hint concrete classes (LibraryNamespaceResolver,PatternDiscovery), but those classes are only registered inpatternkit.services.ymlunder string IDs, not their class names. The DI compiler could not resolve them, so every test that bootstrapped the container failed.The fix adds two class-name aliases to
patternkit.services.ymlso the compiler can find the services. This follows the same pattern already used inpatternkit_usage_tracking.services.yml.PHPStan variable.undefined in ThemeHooks
$section_storageinThemeHooks::loadComponentFromTempStorage()was only assigned inside anifblock. PHPStan correctly flagged it as potentially undefined. This was a pre-existing issue on 9.1.x that CI caught during this review.LegacyHook bridges for Drupal 10 compatibility
HookCollectorPassauto-discovery ofsrc/Hook/classes in submodule directories does not work reliably on Drupal 10.6. With the procedural functions removed, neitherhook_rebuildnorhook_element_info_alterfired on Drupal 10, breaking all FunctionalJavascript Media Library tests in the D10 CI matrix.The fix restores both procedural functions as
#[LegacyHook]bridges, matching the pattern every other hook inpatternkit.modulealready uses. On Drupal 10 the procedural function runs. On Drupal 11 the OOP method takes over.The regression was reproduced locally on Drupal 10.6 and confirmed fixed before pushing. Both D10 and D11 CI matrices passed on the final run.
Comment #7
sluceroThis has been merged for inclusion in the 9.1.3 release.
See #3542304: Patternkit 9.1.3 Release Plan.