0

I am trying to use Cdn service provided in CDN module version 4.0.1 in my custom module Until 3.x version we had service id as cdn.settings

But from 4.x version I can see there is no service id

https://git.drupalcode.org/project/cdn/-/blob/4.x/cdn.services.yml?ref_type=heads

I need to pass cdn.settings service id as an argument in my custom service but I am not able to pass due to missing service id as shown below , when I pass cdn.settings in argument it gives an error, what should be the solution for the same ?

    class: 'Drupal\abc_views\CDNWrapperService'
    arguments: ["@cdn.settings", "@request_stack"]```

1 Answer 1

2

These are the first two entries in the services file you have linked:

services:
  Drupal\cdn\CdnSettings:
    arguments: ['@config.factory']

  Drupal\cdn\File\FileUrlGenerator:
    public: false
    decorates: file_url_generator
    arguments:
      - '@Drupal\cdn\File\FileUrlGenerator.inner'
      - '%app.root%'
      - '@stream_wrapper_manager'
      - '@request_stack'
      - '@private_key'
      - '@Drupal\cdn\CdnSettings'

You can see in the second service definition that the last argument is referencing the first service.

Using the class name as service ID makes it easier to autowire services, although Drupal didn't take this route for core services, they implemented aliases. See https://www.drupal.org/node/3323122

4
  • when I try to add it under .services.yml file arguments: ["@Drupal\cdn\CdnSettings", "@request_stack"]
    – harshal
    Commented Sep 4, 2023 at 6:40
  • it throws an error `` Found unknown escape character "\c" at line 26 (near "arguments: ["@Drupal\cdn\CdnSettings", "@request_stack"]"). ``
    – harshal
    Commented Sep 4, 2023 at 6:40
  • It worked when I used single quotes instead of double quotes
    – harshal
    Commented Sep 4, 2023 at 6:42
  • 1
    Of course, if in doubt always use single quotes. You didn't get the hint, though. After the module changed the service ID you can use the option autowire: true instead of arguments: ....
    – 4uk4
    Commented Sep 5, 2023 at 7:23

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.