Problem/Motivation

This module supports ^10 || ^11. Drupal 11.1 introduced native Object-Oriented Hooks using PHP attributes. We want to adopt this modern pattern while maintaining support for Drupal 10 and 11.0.

Proposed resolution

Implement the "Shim" pattern recommended in Drupal 11.1 OO Hooks Change Record.

  1. Create Hook Class:
    • Create src/Hook/UnicodeSoftHyphensHooks.php.
    • Move the logic from unicode_soft_hyphens_form_alter to UnicodeSoftHyphensHooks::formAlter.
    • Use the #[Hook('form_alter')] attribute on the method.
    • Register this class as a service in unicode_soft_hyphens.services.yml with autowire: true (required for D10/11.0 to use it via service container, though D11.1+ discovers it automatically).
  2. Update .module (Legacy Shim):
    • Keep unicode_soft_hyphens_form_alter() in the .module file.
    • Add use Drupal\Core\Hook\Attribute\LegacyHook;.
    • Add #[LegacyHook] attribute to the function. This tells Drupal 11.1+ to ignore this procedural function and use the OO Hook instead.
    • Inside the function, delegate to the service:
// @phpstan-ignore-next-line
#[LegacyHook]
function unicode_soft_hyphens_form_alter(&$form, FormStateInterface $form_state, $form_id) {
   \Drupal::service(UnicodeSoftHyphensHooks::class)->formAlter($form, $form_state, $form_id);
}
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

guido_s created an issue. See original summary.

guido_s’s picture

Issue summary: View changes
guido_s’s picture

Issue summary: View changes
scontzen’s picture

Assigned: Unassigned » scontzen

scontzen’s picture

Assigned: scontzen » guido_s
Status: Active » Needs review

Converted hook_form_alter() to OO hooks using the #[Hook] attribute.

Changes:
- Moved all logic from unicode_soft_hyphens.module into src/Hook/UnicodeSoftHyphensHooks.php
- Added #[LegacyHook] bridge in .module for backward compatibility with Drupal < 10.3
- Added unicode_soft_hyphens.services.yml to register the hook class
- Replaced global t() calls with $this->t() via StringTranslationTrait
- Fixed copy-paste error in docblock (node type builder comment said "block type config")

The procedural hook is kept as a #[LegacyHook] bridge so the module remains compatible with ^10 || ^11. Tested locally — config forms and soft hyphen button work as before.

guido_s’s picture

I checked the code and tested it.
I added 1 commit to improve performance.
Seems to me you converted the hooks correctly but on testing I also noticed the feature isn't working in the layout builder as intended.
I know we had that working in a project, so I'm currently not sure if anything in core changed or in our module that broke the feature in layout builder. But as this is not related to this issue, I'll merge it and open a follow up issue to fix functionality in layout builder.

guido_s’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.

  • guido_s committed a6c114cd on 1.0.x authored by scontzen
    feat: #3575727 Convert hook_form_alter implementations to OOP Hooks
    
    By...
guido_s’s picture

Status: Fixed » Closed (fixed)

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