It's all in the title.

Issue fork toc_js-3574036

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

Convert all hooks to OOP format

Converted all hook implementations across the main module and all sub-modules from procedural .module functions to OOP classes using #[Hook] attributes, with #[LegacyHook] delegations in .module files for Drupal 10.6 backward compatibility.

toc_js (main module) — 7 hooks converted

  • Created src/Hook/TocJsHooks.php with #[Hook] attributes for: help, theme, theme_suggestions_toc_js_alter, entity_extra_field_info, node_view, form_node_type_form_alter + static entity builder method
  • Updated toc_js.services.yml to register the hooks class with toc_js.service and current_user dependencies
  • Updated toc_js.module with #[LegacyHook] delegations
  • Fixed broken hook_help() implementation — function was named toc_js_node_help() instead of toc_js_help(), so it was never called
  • Kept template_preprocess_toc_js() as-is (empty theme preprocess, not a hook)

toc_js_filter (sub-module) — 1 hook converted

  • Created src/Hook/TocJsFilterHooks.php with #[Hook('help')]
  • Created toc_js_filter.services.yml
  • Updated toc_js_filter.module with #[LegacyHook]

toc_js_per_node (sub-module) — 6 hooks converted

  • Created src/Hook/TocJsPerNodeHooks.php with #[Hook] attributes for: help, entity_extra_field_info, form_node_type_form_alter, entity_bundle_field_info, entity_base_field_info, form_node_form_alter + static entity builder method
  • Created toc_js_per_node.services.yml with current_user dependency
  • Updated toc_js_per_node.module with #[LegacyHook] delegations

toc_js_test_module — 1 hook converted

  • Created src/Hook/TocJsTestModuleHooks.php with #[Hook('preprocess_node')]
  • Created toc_js_test_module.services.yml
  • Updated toc_js_test_module.module with #[LegacyHook]

Test results

Existing FunctionalJavascript test passes (1 test, 12 assertions). Cache rebuild clean.

  • mably committed ac498700 on 3.x
    task: #3574036 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.

mably’s picture

Convert all hooks to OOP format using #[Hook] attributes with #[LegacyHook] backward compatibility for Drupal 10.x, across the main module and both sub-modules (toc_js_filter, toc_js_per_node). Also remove the empty no-op toc_js_requirements() hook.

Changes

toc_js.module

  • All 7 hook functions (hook_help, hook_theme, hook_theme_suggestions_toc_js_alter, hook_entity_extra_field_info, hook_node_view, hook_form_node_type_form_alter, entity builder) now carry #[LegacyHook] and delegate to TocJsHooks OOP service.
  • Fixed incorrect function name toc_js_node_help -> toc_js_help.

toc_js.install

  • Removed the empty no-op toc_js_requirements() function (returned an empty array for all phases).

src/Hook/TocJsHooks.php (new)

  • OOP hook class with dependency injection (TocJsService, AccountProxyInterface).
  • Implements #[Hook('help')], #[Hook('theme')], #[Hook('theme_suggestions_toc_js_alter')], #[Hook('entity_extra_field_info')], #[Hook('node_view')], #[Hook('form_node_type_form_alter')].
  • Static formNodeTypeFormBuilder() entity builder.

toc_js.services.yml

  • Registered Drupal\toc_js\Hook\TocJsHooks with explicit arguments (@toc_js.service, @current_user).

modules/toc_js_filter/toc_js_filter.module

  • hook_help now carries #[LegacyHook] and delegates to TocJsFilterHooks.

modules/toc_js_filter/src/Hook/TocJsFilterHooks.php (new)

  • OOP hook class implementing #[Hook('help')].

modules/toc_js_filter/toc_js_filter.services.yml (new)

  • Registered Drupal\toc_js_filter\Hook\TocJsFilterHooks.

modules/toc_js_per_node/toc_js_per_node.module

  • All 6 hook functions (hook_help, hook_entity_extra_field_info, hook_form_node_type_form_alter, hook_entity_bundle_field_info, hook_entity_base_field_info, hook_form_node_form_alter) now carry #[LegacyHook] and delegate to TocJsPerNodeHooks.

modules/toc_js_per_node/src/Hook/TocJsPerNodeHooks.php (new)

  • OOP hook class with dependency injection (AccountProxyInterface).
  • All hooks converted with proper #[Hook] attributes and $this->t() instead of procedural t().

modules/toc_js_per_node/toc_js_per_node.info.yml

  • Added missing drupal:node dependency (required by the hook class).

modules/toc_js_per_node/toc_js_per_node.services.yml (new)

  • Registered Drupal\toc_js_per_node\Hook\TocJsPerNodeHooks with @current_user argument.

tests/modules/toc_js_test_module/toc_js_test_module.module

  • hook_preprocess_node now carries #[LegacyHook] and delegates to TocJsTestModuleHooks.

tests/modules/toc_js_test_module/src/Hook/TocJsTestModuleHooks.php (new)

  • OOP hook class implementing #[Hook('preprocess_node')].

tests/modules/toc_js_test_module/toc_js_test_module.services.yml (new)

  • Registered Drupal\toc_js_test_module\Hook\TocJsTestModuleHooks.

.gitlab-ci.yml

  • Disabled PHPStan on previous major (only interested in current version).

Backward compatibility

  • On Drupal 10.x: #[LegacyHook] is ignored, procedural functions run normally and delegate to OOP services.
  • On Drupal 11.3+: #[LegacyHook] prevents procedural functions from running, #[Hook] attributes on OOP methods are called directly by the hook system.

Status: Fixed » Closed (fixed)

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