diff --git a/dependent_fields.module b/dependent_fields.module
index 68c0023..b27d82b 100644
--- a/dependent_fields.module
+++ b/dependent_fields.module
@@ -5,94 +5,14 @@
  * Dependent fields module.
  */
 
-use Drupal\Core\Entity\EntityFormInterface;
-use Drupal\Core\Entity\EntityInterface;
-use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
-use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
+use Drupal\Core\Hook\Attribute\LegacyHook;
+use Drupal\dependent_fields\Hook\DependentFieldsHooks;
 use Drupal\Core\Form\FormStateInterface;
 
 /**
  * Implements hook_field_widget_single_element_form_alter().
  */
+#[LegacyHook]
 function dependent_fields_field_widget_single_element_form_alter(&$element, FormStateInterface $form_state, $context) {
-  $form_object = $form_state->getFormObject();
-  // Determine the entity from the available form context.
-  if (isset($context['items']) && $context['items'] instanceof EntityReferenceFieldItemListInterface) {
-    /**
-     * @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
-     */
-    $items = $context['items'];
-    $entity = $items->getEntity();
-  }
-  elseif ($form_object instanceof EntityFormInterface) {
-    $entity = $form_object->getEntity();
-  }
-  else {
-    $entity = new stdClass();
-  }
-
-  if ($entity instanceof EntityInterface) {
-    // Check if the field is a dependent field.
-    /** @var \Drupal\Core\Field\FieldItemList $items */
-    $items = $context['items'];
-    $current_field = $items->getFieldDefinition()->getName();
-
-    // Check if field is configured as parent of one dependent field.
-    if (method_exists($entity, 'getFieldDefinitions')) {
-      $fields_definitions = $entity->getFieldDefinitions();
-
-      // Check if current field has children.
-      $child_fields = [];
-      foreach ($fields_definitions as $field_name => $field_definition) {
-        $handler = $field_definition->getSetting('handler');
-        if ($handler == 'dependent_fields_selection') {
-          $handle_settings = $field_definition->getSetting('handler_settings');
-
-          if (isset($handle_settings['dependent_fields_view']['parent_field'])) {
-            $parent_field = $handle_settings['dependent_fields_view']['parent_field'];
-            $has_children = ($parent_field == $current_field);
-
-            if ($has_children) {
-              $child_fields[] = $field_name;
-            }
-          }
-        }
-      }
-
-      // Add ajax to parent field.
-      if (count($child_fields)) {
-        $children = $element['#ajax']['dependent_field_children'] ?? [];
-        $children += $child_fields;
-
-        $ajax_definition = [
-          'callback' => '\Drupal\dependent_fields\Plugin\EntityReferenceSelection\ViewsSelection::updateDependentField',
-          'event' => 'change',
-          'progress' => [
-            'type' => 'throbber',
-          ],
-          'dependent_field_children' => $children,
-        ];
-
-        if (!empty($context['widget']) && $context['widget'] instanceof EntityReferenceAutocompleteWidget && !empty($element['target_id'])) {
-          $ajax_definition['event'] = 'autocompleteclose';
-          $element['target_id']['#ajax'] = $ajax_definition;
-        }
-        else {
-          $element['#ajax'] = $ajax_definition;
-        }
-
-        $element['#attached']['library'][] = 'dependent_fields/dependentField';
-      }
-      // Make sure multi value dependent fields stay multi value,
-      // even if they don't have any options initially.
-      if (isset($entity->getFieldDefinitions()[$current_field]) &&
-        $entity->getFieldDefinitions()[$current_field]->getFieldStorageDefinition()->getCardinality() === -1 &&
-        $entity->getFieldDefinitions()[$current_field]->getSetting('handler') === 'dependent_fields_selection' &&
-        in_array($element['#type'], ['select', 'select2']) &&
-        !$element['#multiple']
-      ) {
-        $element['#multiple'] = TRUE;
-      }
-    }
-  }
+  \Drupal::service(DependentFieldsHooks::class)->fieldWidgetSingleElementFormAlter($element, $form_state, $context);
 }
diff --git a/dependent_fields.services.yml b/dependent_fields.services.yml
new file mode 100644
index 0000000..05845a5
--- /dev/null
+++ b/dependent_fields.services.yml
@@ -0,0 +1,5 @@
+
+services:
+  Drupal\dependent_fields\Hook\DependentFieldsHooks:
+    class: Drupal\dependent_fields\Hook\DependentFieldsHooks
+    autowire: true
diff --git a/src/Hook/DependentFieldsHooks.php b/src/Hook/DependentFieldsHooks.php
new file mode 100644
index 0000000..861699c
--- /dev/null
+++ b/src/Hook/DependentFieldsHooks.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace Drupal\dependent_fields\Hook;
+
+use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\EntityFormInterface;
+use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Hook\Attribute\Hook;
+
+/**
+ * Hook implementations for dependent_fields.
+ */
+class DependentFieldsHooks {
+
+  /**
+   * Implements hook_field_widget_single_element_form_alter().
+   */
+  #[Hook('field_widget_single_element_form_alter')]
+  public static function fieldWidgetSingleElementFormAlter(&$element, FormStateInterface $form_state, $context) {
+    $form_object = $form_state->getFormObject();
+    // Determine the entity from the available form context.
+    if (isset($context['items']) && $context['items'] instanceof EntityReferenceFieldItemListInterface) {
+      /**
+       * @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
+       */
+      $items = $context['items'];
+      $entity = $items->getEntity();
+    }
+    elseif ($form_object instanceof EntityFormInterface) {
+      $entity = $form_object->getEntity();
+    }
+    else {
+      $entity = new \stdClass();
+    }
+    if ($entity instanceof EntityInterface) {
+      // Check if the field is a dependent field.
+      /** @var \Drupal\Core\Field\FieldItemList $items */
+      $items = $context['items'];
+      $current_field = $items->getFieldDefinition()->getName();
+      // Check if field is configured as parent of one dependent field.
+      if (method_exists($entity, 'getFieldDefinitions')) {
+        $fields_definitions = $entity->getFieldDefinitions();
+        // Check if current field has children.
+        $child_fields = [];
+        foreach ($fields_definitions as $field_name => $field_definition) {
+          $handler = $field_definition->getSetting('handler');
+          if ($handler == 'dependent_fields_selection') {
+            $handle_settings = $field_definition->getSetting('handler_settings');
+            if (isset($handle_settings['dependent_fields_view']['parent_field'])) {
+              $parent_field = $handle_settings['dependent_fields_view']['parent_field'];
+              $has_children = $parent_field == $current_field;
+              if ($has_children) {
+                $child_fields[] = $field_name;
+              }
+            }
+          }
+        }
+        // Add ajax to parent field.
+        if (count($child_fields)) {
+          $children = $element['#ajax']['dependent_field_children'] ?? [];
+          $children += $child_fields;
+          $ajax_definition = [
+            'callback' => '\Drupal\dependent_fields\Plugin\EntityReferenceSelection\ViewsSelection::updateDependentField',
+            'event' => 'change',
+            'progress' => [
+              'type' => 'throbber',
+            ],
+            'dependent_field_children' => $children,
+          ];
+          if (!empty($context['widget']) && $context['widget'] instanceof EntityReferenceAutocompleteWidget && !empty($element['target_id'])) {
+            $ajax_definition['event'] = 'autocompleteclose';
+            $element['target_id']['#ajax'] = $ajax_definition;
+          }
+          else {
+            $element['#ajax'] = $ajax_definition;
+          }
+          $element['#attached']['library'][] = 'dependent_fields/dependentField';
+        }
+        // Make sure multi value dependent fields stay multi value,
+        // even if they don't have any options initially.
+        if (isset($entity->getFieldDefinitions()[$current_field]) && $entity->getFieldDefinitions()[$current_field]->getFieldStorageDefinition()->getCardinality() === -1 && $entity->getFieldDefinitions()[$current_field]->getSetting('handler') === 'dependent_fields_selection' && in_array($element['#type'], [
+          'select',
+          'select2',
+        ]) && !$element['#multiple']) {
+          $element['#multiple'] = TRUE;
+        }
+      }
+    }
+  }
+
+}
