Convert Drupal Hooks to Object‑Oriented Methods Using Rector
In a recent tutorial, Joachim Noreiko outlines how to modernise Drupal codebases by converting procedural hook implementations into object-oriented methods using Rector. He demonstrates how to install the palantirnet/drupal-rector package, activate the HookConvertRector rule, and run it against a module to migrate hook logic into dedicated Hook classes. The process leaves behind legacy wrappers for compatibility but moves core functionality into class methods.
Once the conversion is complete, Joachim recommends cleaning up the generated code using PHPCS with the Drupal coding standard. This addresses formatting inconsistencies and leftover import statements, particularly in the .module file and newly created Hook classes. He provides exact commands for scoped reformatting.
Developers are encouraged to refine the result further: separating hooks into multiple classes, merging related logic, or internalising helper functions to improve maintainability. While the article focuses on the Computed Field module, the workflow is adaptable to other custom codebases, adjusting to recent Drupal core changes.

