interface FileUrlGeneratorInterface (View source)

Generates file URLs for a stream to an external or local file.

Compatibility: normal paths and stream wrappers.

There are two kinds of local files:

  • "managed files", i.e. those stored by a Drupal-compatible stream wrapper. These are files that have either been uploaded by users or were generated automatically (for example through CSS aggregation).
  • "shipped files", i.e. those outside of the files directory, which ship as part of Drupal core or contributed modules or themes.

Separate methods are provided to provide absolute and relative URLs as well as plain strings or Url objects, depending on the requirements. In general, it is recommended to always use relative URLs unless absolute URL's are required.

Methods

string
generateString(string $uri)

Creates a root-relative web-accessible URL string.

string
generateAbsoluteString(string $uri)

Creates an absolute web-accessible URL string.

Url
generate(string $uri)

Creates a root-relative web-accessible URL object.

string
transformRelative(string $file_url, bool $root_relative = TRUE)

Transforms an absolute URL of a local file to a relative URL.

Details

string generateString(string $uri)

Creates a root-relative web-accessible URL string.

Parameters

string $uri

The URI to a file for which we need an external URL, or the path to a shipped file.

Return Value

string

For a local URL (matching domain), a root-relative string containing a URL that may be used to access the file. An absolute URL may be returned when using a CDN or a remote stream wrapper.

Exceptions

InvalidStreamWrapperException

string generateAbsoluteString(string $uri)

Creates an absolute web-accessible URL string.

Parameters

string $uri

The URI to a file for which we need an external URL, or the path to a shipped file.

Return Value

string

An absolute string containing a URL that may be used to access the file.

Exceptions

InvalidStreamWrapperException

Url generate(string $uri)

Creates a root-relative web-accessible URL object.

Parameters

string $uri

The URI to a file for which we need an external URL, or the path to a shipped file.

Return Value

Url

For a local URL (matching domain), a base-relative Url object containing a URL that may be used to access the file. An Url object with absolute URL may be returned when using a CDN or a remote stream wrapper. Use setAbsolute() on the Url object to build an absolute URL.

Exceptions

InvalidStreamWrapperException

string transformRelative(string $file_url, bool $root_relative = TRUE)

Transforms an absolute URL of a local file to a relative URL.

May be useful to prevent problems on multisite set-ups and prevent mixed content errors when using HTTPS + HTTP.

Parameters

string $file_url

A file URL of a local file as generated by \Drupal\Core\File\FileUrlGenerator::generate().

bool $root_relative

(optional) TRUE if the URL should be relative to the root path or FALSE if relative to the Drupal base path.

Return Value

string

If the file URL indeed pointed to a local file and was indeed absolute, then the transformed, relative URL to the local file. Otherwise: the original value of $file_url.