Change record status: 
Project: 
Introduced in branch: 
9.0.x
Introduced in version: 
9.0.0
Description: 

The app.root and site.path services have been converted to container parameters.

The services are deprecated in Drupal 9 and will be removed in Drupal 10.

Service YAML file will need to be updated. For example:

Before

   cache.backend.apcu:
     class: Drupal\Core\Cache\ApcuBackendFactory
     arguments: ['@app.root', '@site.path', '@cache_tags.invalidator.checksum']

After

   cache.backend.apcu:
     class: Drupal\Core\Cache\ApcuBackendFactory
     arguments: ['%app.root%', '%site.path%', '@cache_tags.invalidator.checksum']

How to be cross-compatible with Drupal 8, 9 and 10?

This is how you can be compatible with Drupal 8 and 9 while not triggering any Drupal 10 deprecation notices (i.e. be compatible with Drupal 8, 9 and 10):

diff --git a/cdn.services.yml b/cdn.services.yml
index da9db27..c12756d 100644
--- a/cdn.services.yml
+++ b/cdn.services.yml
@@ -5,7 +5,7 @@ services:
 
   cdn.file_url_generator:
     class: Drupal\cdn\File\FileUrlGenerator
-    arguments: ['@app.root', '@stream_wrapper_manager', '@request_stack', '@private_key', '@cdn.settings']
+    arguments: ['%app.root%', '@stream_wrapper_manager', '@request_stack', '@private_key', '@cdn.settings']
 
   # Event subscribers.
   cdn.config_subscriber:
diff --git a/src/CdnServiceProvider.php b/src/CdnServiceProvider.php
index aec1a4b..365b133 100644
--- a/src/CdnServiceProvider.php
+++ b/src/CdnServiceProvider.php
@@ -29,6 +29,12 @@ class CdnServiceProvider implements ServiceProviderInterface {
         //   banned, without having to run this middleware.
         ->addTag('http_middleware', ['priority' => 230]);
     }
+    // @todo Delete this when dropping Drupal 8 support in https://www.drupal.org/project/cdn/issues/3103682.
+    if (version_compare(\Drupal::VERSION, '9.0', '<')) {
+      // @see https://www.drupal.org/project/drupal/issues/3074585
+      $container->getDefinition('cdn.file_url_generator')
+        ->setArgument(0, new Reference('app.root'));
+    }
   }
 
   /**
Impacts: 
Site builders, administrators, editors
Module developers
Site templates, recipes and distribution developers

Comments

mmatsoo’s picture

If you find yourself on this change record page, wondering how to find the active site in a multisite set-up and you're used to using the deprecated site.path service. Below is a way to get the active site, using the static function DrupalKernel::findSitePath().

// This gives something like "sites/sitename".
$site_path = DrupalKernel::findSitePath(\Drupal::request());
$site_path = explode('/', $site_path);
$active_site = $site_path[1];
dmitry.korhov’s picture

Another way to retrieve path by using parameter directly from container:

  $site_path = \Drupal::getContainer()->getParameter('site.path');
  $site_path = explode('/', $site_path);
  $site_name = $site_path[1];