By nickolaj on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
9.3.x
Introduced in version:
9.3.0
Description:
Functions file_create_url() and file_url_transform_relative() are deprecated.
Instead, get file_url_generatorservice injected into your service or controller from the container and call its methods.
| Before | After |
|---|---|
file_create_url($uri) |
\Drupal::service('file_url_generator')->generateAbsoluteString($uri) |
file_url_transform_relative($file_url) |
\Drupal::service('file_url_generator')->transformRelative($file_url) |
file_url_transform_relative(file_create_url($uri)) |
\Drupal::service('file_url_generator')->generateString($uri) |
Drupal\Core\Url::fromUri(file_create_url($uri)) |
\Drupal::service('file_url_generator')->generate($uri) |
The new service 'file_url_generator' is providing the following new methods:
-
The method
generateString()which creates a root-relative web-accessible URL string; -
The method
generateAbsoluteString()which creates an absolute web-accessible URL string; -
The method
generate()which creates a root-relative web-accessible URL object; -
The method
transformRelative()which transforms an absolute URL of a local file to a relative URL.
In addition, Drupal\Core\Asset\CssOptimizer (this is providing the 'asset.css.optimizer' service), Drupal\Core\Asset\JsCollectionRenderer (that is providing the 'asset.js.collection_renderer' service) and Drupal\Core\Asset\CssCollectionRenderer (that is providing the 'asset.css.collection_renderer' service) are now taking the FileUrlGeneratorInterface constructor as an argument.
Impacts:
Module developers
Site templates, recipes and distribution developers