I need to do a couple of things:
- Inject guzzle's httpclient into class A
- Inject class A into my service
What I've tried so far:
-
services: my_module.field_fetcher: class: Drupal\my_module\ClassB arguments: ['@my_module.obj', '@logger.factory', '@config.factory'] my_module.obj: class: Drupal\my_module\ClassA arguments: ['@http.client']
-
parameters: my_module.http_client.config: base_uri: 'http://myurl.com/' services: my_module.http_client: factory: '@http_client_factory:fromOptions' arguments: ['%my_module.http_client.config%'] my_module.obj: class: Drupal\my_module\ClassA arguments: ['@my_module.http_client'] my_module.field_fetcher: class: Drupal\my_module\ClassB arguments: ['@my_module.obj', '@logger.factory', '@config.factory']
Simple versions of classes A and B look like this:
ClassA:
<?php
namespace Drupal\my_module;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class A {
/**
* @var \GuzzleHttp\ClientInterface
*/
private $httpClient;
public function __construct(ClientInterface $httpClient) {
$this->httpClient = $httpClient;
}
}
ClassB:
<?php
namespace Drupal\my_module;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
use Drupal\Core\Logger\LoggerChannelFactory;
class B implements BInterface {
/**
* @var \Drupal\my_module\A
*/
private $a;
/**
* @var \Drupal\Core\Logger\LoggerChannelFactory
*/
private $logger;
public function __construct(A $a, LoggerChannelFactory $logger, ConfigFactoryInterface $config) {
$this->a = $a;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static(
A::create($container),
$container->get('my_module.obj'),
$container->get('logger.factory')->get('my_module'),
$container->get('config.factory')
);
}
}
No matter what I try, I keep running into the same issue of drupal error logs telling me that "argument 1 passed to Drupal\my_module\ClassB::__construct() must be an instance of Drupal\my_module\ClassA, string given"
Edit: Updated classes A and B to reflect changes suggested.