Hello,

While reviewing the recent OOP hook implementations using the #[Hook] attribute (introduced in Drupal 11.1), I noticed that some hook classes still retrieve services using \Drupal::service() instead of constructor dependency injection.

For example:

#[Hook('webform_admin_third_party_settings_form_alter', module: 'antibot')]
public function antibotWebformAdminThirdPartySettingsFormAlter(&$form, FormStateInterface $form_state) {
  /** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
  $third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');

  $antibot = $third_party_settings_manager->getThirdPartySetting('antibot', 'antibot');
  $antibot_state = WEBFORM_ANTIBOT_NEUTRAL;

  _webform_antibot_form($form, $form_state, $antibot, $antibot_state, $this->t('all webforms'));
}

Since hook classes are services, it seems possible to inject the dependency through the constructor instead, for example:

public function __construct(
  protected WebformThirdPartySettingsManagerInterface $thirdPartySettingsManager
) {}

and then use it inside the hook method.

Is there any plan or discussion about gradually adopting dependency injection in these hook classes, or is the current pattern using \Drupal::service() intentional (for example for compatibility or architectural reasons)?

Thanks for all the work maintaining Webform.

Comments

diegodz created an issue. See original summary.

cilefen’s picture

Category: Feature request » Task
Status: Active » Closed (duplicate)

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.