1

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->entity_query = $query_factory;
    $this->entity_type_manager = $entity_type_manager;

    $this->param1 = $param1;
    $this->param2 = $param2;

  }

public static function create(ContainerInterface $container) {
   return new static(
       $container->get('entity.query'),
       $container->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.

0

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->entity_query = $query_factory;
    $this->entity_type_manager = $entity_type_manager;
    $this->custom_service = $custom_service;
  }

public static function create(ContainerInterface $container) {
   return new static(
       $container->get('entity.query'),
       $container->get('entity_type.manager'),
       $container->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->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->entity_query = $query_factory;
    $this->entity_type_manager = $entity_type_manager;
    $this->custom_service = $custom_service;
    $custom_service->setParam1($param1);
    $custom_service->setParam2($param2);
  }

public static function create(ContainerInterface $container) {
   return new static(
       $container->get('entity.query'),
       $container->get('entity_type.manager'),
       $container->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->entity_query = $query_factory;
    $this->entity_type_manager = $entity_type_manager;
    $this->custom_service = $custom_service;
    $custom_service->setParam1($param1);
    $custom_service->setParam2($param2);
  }

public static function create(ContainerInterface $container) {
   return new static(
       $container->get('entity.query'),
       $container->get('entity_type.manager'),
       $container->get('custom.service'),
       'param1 value',
       'param2 value',
    );
}
  • if the OP wants to change the class to a service this answer from you seems even more appropriate drupal.stackexchange.com/questions/226200/… (not the interface part, but that you don't need create() and the container factory injection) – 4k4 May 30 '17 at 19:12
  • no, my question is differently: I would like to pass parameters to my CustomService. When implementing as a normal class I would use sth. like: myService = new CustomService($param1, $param2); how can I do this with a service? How can I pass parameters when calling \Drupal::service('CustomService') ? – Thomas524 May 31 '17 at 8:35
  • @Thomas524 I updated my answer. – kiamlaluno May 31 '17 at 8:50
  • Thank you. So this means the parameters have to be set by the service itself in the create method? There is no possibility to pass them when I instantiate the service? This would mean that services are not suitable in cases where I need to pass parameters to the constructor? – Thomas524 May 31 '17 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.