How do I pass parameters to a service? How can parameters be passed to service? I have a class that I would like to change to a service and load other services from the container. What I don't understand is how to deal with other parameters in the constructor when calling the service.

For example, I have a CustomService class.

public function __construct(QueryFactory $query_factory, EntityTypeManagerInterface $entity_type_manager, param1, param2) {

    $this-&gt;entity_query = $query_factory;
    $this-&gt;entity_type_manager = $entity_type_manager;

    $this-&gt;param1 = $param1;
    $this-&gt;param2 = $param2;

  }

public static function create(ContainerInterface $container) {
   return new static(
       $container-&gt;get('entity.query'),
       $container-&gt;get('entity_type.manager')
    );
}


How can I instantiate the service I would instantiate with \Drupal::service('CustomService')? It does not work for me; it only works when I remove the custom parameters from the constructor.

This might be a wrong approach, but I'm new to Drupal 8 and would like to use proper coding with dependency injection.
Just add a new parameter in the constructor declaration, and get the service from $container.

public function __construct(QueryFactory $query_factory, EntityTypeManagerInterface $entity_type_manager, CustomServiceInterface custom_service) {
    $this-&gt;entity_query = $query_factory;
    $this-&gt;entity_type_manager = $entity_type_manager;
    $this-&gt;custom_service = $custom_service;
  }

public static function create(ContainerInterface $container) {
   return new static(
       $container-&gt;get('entity.query'),
       $container-&gt;get('entity_type.manager'),
       $container-&gt;get('custom.service')
    );
}


Notice that all the parameters the constructor gets are the ones passed in new static(). I removed $param1 and $param2, since there aren't equivalents in the static() call.

As for passing parameters to the custom.service service, you cannot with $container-&gt;get('custom.service') nor \Drupal::service('custom.service') since both the methods accepts only the service ID as parameter. The service needs a method to set its properties; in that case you could use the following code.

public function __construct(QueryFactory $query_factory, EntityTypeManagerInterface $entity_type_manager, CustomServiceInterface custom_service) {
    $this-&gt;entity_query = $query_factory;
    $this-&gt;entity_type_manager = $entity_type_manager;
    $this-&gt;custom_service = $custom_service;
    $custom_service-&gt;setParam1($param1);
    $custom_service-&gt;setParam2($param2);
  }

public static function create(ContainerInterface $container) {
   return new static(
       $container-&gt;get('entity.query'),
       $container-&gt;get('entity_type.manager'),
       $container-&gt;get('custom.service')
    );
}


You could also use the following code.

public function __construct(QueryFactory $query_factory, EntityTypeManagerInterface $entity_type_manager, CustomServiceInterface custom_service, $param1, $param2) {
    $this-&gt;entity_query = $query_factory;
    $this-&gt;entity_type_manager = $entity_type_manager;
    $this-&gt;custom_service = $custom_service;
    $custom_service-&gt;setParam1($param1);
    $custom_service-&gt;setParam2($param2);
  }

public static function create(ContainerInterface $container) {
   return new static(
       $container-&gt;get('entity.query'),
       $container-&gt;get('entity_type.manager'),
       $container-&gt;get('custom.service'),
       'param1 value',
       'param2 value',
    );
}