0

I just want to inject a custom module class A as a dependency injection for a custom service. I tried but I failed. Can any one please guide me. I'm using Drupal version 10.

Here is my code.

moduleb.services.yml

services:
  classb.brokeragency:
    class: Drupal\customb\Services\BrokerAgency
    arguments: [Twn\SoapClient\ClientBuilder]

BrokerAgency.php

<?php 

namespace Drupal\customb\Services;

use \Twn\SoapClient\ClientBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;


class BrokerAgency implements BrokerAgencyInterface {

    protected ClientBuilder $clientBuilder;


    /**
     * @param ClientBuilder $client_builders
     *
     */
    public function __construct(ClientBuilder $client_builder)
    {
        $this->clientBuilder = $client_builder;
    }

    public static function create(ContainerInterface $container) {
        return new static(
          $client_builder
        );
    }
}

But this is giving following error.

enter image description here

Please help me to solve this issue. Thanks in advance.

PS - Does Drupal allow this ? If no, then is there any way to achieve. this.

1

1 Answer 1

3

You need to create a service/ factory for Twn\SoapClient\ClientBuilder to create a new ClientBuilder object and use this service/ factory in your services.yml. To reference a service, you'll need to use the following notation.

services:
  classb.brokeragency:
    class: Drupal\customb\Services\BrokerAgency
    arguments: ['@my.client.builder']

See Services and dependency injection in Drupal 8+ for more examples and documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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