Autowire every service and inject dependencies instead of calling \Drupal

Problem/Motivation

The main module, ai_disclosure_audit and ai_disclosure_csv_export declare their services with autowire: true. ai_disclosure_image (11 services) and ai_disclosure_av (24 services) are not autowired, and most of their definitions list the constructor arguments by hand. ai_disclosure_workflow is mixed. Hand-written argument lists drift from the constructors and make every new dependency a change in two places.

Some classes also fetch services with \Drupal::service() or \Drupal::entityTypeManager() at runtime instead of receiving them: the field widget, the icon variant preview, the statement URI helper and the "remove from every bundle" form.

Proposed resolution

  • Every service of the project is autowired. Module interfaces get an alias in services.yml so autowiring can resolve them. Scalars, logger channels and tagged lists use #[Autowire] and #[AutowireIterator] on the constructor, or a single named argument next to autowire: true.
  • Classes that Drupal builds through the container (services, hook classes, forms, controllers, plugins) receive their dependencies through the constructor, create() or AutowireTrait. No \Drupal:: call is left in them.
  • \Drupal:: stays only where Drupal offers no injection, and each case gets a one-line comment saying why:
    • procedural code in .module, .install and .post_update.php, including the hook shims kept for Drupal 10.4;
    • entity and field item classes (config entities, AiDisclosureItem, the derivative saved event trait).
  • Tests may keep using \Drupal:: and $this->container->get().

Service ids, public method signatures and config are unchanged.

Remaining tasks

  • ai_disclosure_image services autowired
  • ai_disclosure_av services autowired
  • ai_disclosure_workflow services autowired
  • Runtime \Drupal:: calls removed from widget, icon variant preview, statement URI helper and the remove form
  • A kernel test that fails when a service definition of the project lacks autowire: true, and when a class under src/ other than the listed exceptions calls \Drupal::

Acceptance criteria

  • git grep '\\Drupal::' -- src modules/*/src returns only the listed exceptions, each with its comment.
  • Every service definition in the project's *.services.yml (test modules excluded) is autowired.
  • Every service id that existed before still exists and resolves to the same class.
  • Full test suite, PHPStan (current and previous major) and phpcs pass.

API changes

None.

Data model changes

None.

Part of #80 (closed).

AI Compliance

🤖 Generated with the help of an LLM, reviewed and approved by me before posting.

Edited by Giorgio Alfredo Pagano