Skip to main content
deleted 1 character in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217
  1. The standard way to inject services in other services is passing dependencies directly through the constructor:

mymodule.services.yml:

    services:
      mymodule.foo_service:
        class: Drupal\mymodule\FooService
        arguments: ['@current_user']
  1. While you can define a factory method in *.services.yml, see https://symfony.com/doc/current/service_container/factories.html, in Drupal the most common places for this are Controllers and Plugins which implement ContainerInjectionInterface::create or ContainerFactoryPluginInterface::create. See How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

  2. \Drupal::service... is largely used from procedural code when not in an OOP context, like hooks areor static callbacks. See the comment from @Kevin.

  1. The standard way to inject services in other services is passing dependencies directly through the constructor:

mymodule.services.yml:

    services:
      mymodule.foo_service:
        class: Drupal\mymodule\FooService
        arguments: ['@current_user']
  1. While you can define a factory method in *.services.yml, see https://symfony.com/doc/current/service_container/factories.html, in Drupal the most common places for this are Controllers and Plugins which implement ContainerInjectionInterface::create or ContainerFactoryPluginInterface::create. See How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

  2. \Drupal::service... is largely used from procedural code when not in an OOP context, like hooks are static callbacks. See the comment from @Kevin.

  1. The standard way to inject services in other services is passing dependencies directly through the constructor:

mymodule.services.yml:

    services:
      mymodule.foo_service:
        class: Drupal\mymodule\FooService
        arguments: ['@current_user']
  1. While you can define a factory method in *.services.yml, see https://symfony.com/doc/current/service_container/factories.html, in Drupal the most common places for this are Controllers and Plugins which implement ContainerInjectionInterface::create or ContainerFactoryPluginInterface::create. See How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

  2. \Drupal::service... is largely used from procedural code when not in an OOP context, like hooks or static callbacks. See the comment from @Kevin.

Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

  1. The standard way to inject services in other services is passing dependencies directly through the constructor:

mymodule.services.yml:

    services:
      mymodule.foo_service:
        class: Drupal\mymodule\FooService
        arguments: ['@current_user']
  1. While you can define a factory method in *.services.yml, see https://symfony.com/doc/current/service_container/factories.html, in Drupal the most common places for this are Controllers and Plugins which implement ContainerInjectionInterface::create or ContainerFactoryPluginInterface::create. See How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

  2. \Drupal::service... is largely used from procedural code when not in an OOP context, like hooks are static callbacks. See the comment from @Kevin.