Closed (duplicate)
Project:
Webform
Version:
6.3.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
16 Mar 2026 at 20:40 UTC
Updated:
17 Mar 2026 at 12:26 UTC
Jump to comment: Most recent
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
Comment #2
cilefen commentedYes. #3500969: Add Dependency injection to Hook classes.