Summary
Convert all procedural hook implementations in term_glossary.module and term_glossary_per_node.module to OOP classes using #[Hook] attributes (introduced in Drupal 10.3).
This also drops Drupal 9 support by bumping core_version_requirement from ^9 || ^10 || ^11 to ^10 || ^11 in all 4 info.yml files.
Scope
term_glossary.module — 5 hooks + 1 private helper:
hook_helphook_themehook_field_formatter_third_party_settings_formhook_field_formatter_settings_summary_alterhook_preprocess_field_term_glossary_check_formatter()helper → private method
term_glossary_per_node.module — 5 hooks + 2 helpers:
hook_helphook_entity_bundle_field_infohook_form_node_type_form_alterhook_entity_base_field_infohook_form_node_form_alterterm_glossary_per_node_form_node_type_form_builder()entity builder → private method_term_glossary_per_node_node_validate_handler()validation handler → private method
4 info.yml files:
term_glossary.info.ymlterm_glossary_per_node.info.ymlterm_glossary_abbr.info.ymlterm_glossary_tippy.info.yml
Approach
- Create
src/Hook/TermGlossaryHooks.phpandmodules/term_glossary_per_node/src/Hook/TermGlossaryPerNodeHooks.php - Inject services via constructor (e.g.
term_glossary.manager,renderer,entity_type.manager,current_user) - Keep
.modulefiles with#[LegacyHook]delegating to hook classes for Drupal 10.x backward compatibility - Entity builder and validation handler callbacks become private methods on the hook class, referenced as
[$this, 'method']in form alter
Issue fork term_glossary-3576064
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 #3
mably commentedOOP hooks conversion
Converted all procedural hooks in
term_glossaryandterm_glossary_per_nodeto OOP classes using#[Hook]attributes, with#[LegacyHook]delegations in the.modulefiles for Drupal 10.x backward compatibility. Dropped Drupal 9 support (core_version_requirementbumped to^10.3 || ^11).New files
src/Hook/TermGlossaryHooks.php— main module hook class with constructor-injectedTermGlossaryManagerInterfaceandRendererInterface. Hooks converted:hook_helphook_themehook_field_formatter_third_party_settings_formhook_field_formatter_settings_summary_alterhook_preprocess_fieldThe old
_term_glossary_check_formatter()helper becomes a private method on the class.modules/term_glossary_per_node/src/Hook/TermGlossaryPerNodeHooks.php— submodule hook class with constructor-injectedEntityTypeManagerInterfaceandAccountProxyInterface. Hooks converted:hook_helphook_entity_bundle_field_infohook_form_node_type_form_alterhook_entity_base_field_infohook_form_node_form_alterThe old procedural callbacks (
form_builder,validate_handler) become public methods on the class (required because they are referenced as[$this, 'method']callables).Modified files
term_glossary.module— rewritten to#[LegacyHook]stubs delegating toTermGlossaryHooks.modules/term_glossary_per_node/term_glossary_per_node.module— same pattern, delegating toTermGlossaryPerNodeHooks.term_glossary.services.yml— added interface alias (TermGlossaryManagerInterface: '@term_glossary.manager') so autowiring can resolve the dependency, and explicit registration ofTermGlossaryHooksfor Drupal 10.x compatibility (on 11.x,HookCollectorPassauto-registers it but the manual entry is harmless).*.info.ymlfiles —core_version_requirementchanged from^9 || ^10 || ^11to^10.3 || ^11(main module + 3 submodules).New file
modules/term_glossary_per_node/term_glossary_per_node.services.yml— registersTermGlossaryPerNodeHookswithautowire: truefor Drupal 10.x compatibility.Unit tests
tests/src/Unit/Hook/TermGlossaryHooksTest.php— 6 tests, 20 assertions coveringTermGlossaryHooks:text_default) delegates to the manager service; incompatible formatter returns an empty array and the manager is never called.elsebranch and adds "Vocabulary: …".getHandler(); full happy path verifies markup replacement, cache tag merging (config + taxonomy term tags), and library attachment.Dependencies (
TermGlossaryManagerInterface,RendererInterface) are mocked — pure unit tests with no Drupal bootstrap.Comment #5
mably commented