You are here

interface FileUrlGeneratorInterface in Drupal 10

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/File/FileUrlGeneratorInterface.php \Drupal\Core\File\FileUrlGeneratorInterface

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.

Hierarchy

Expanded class hierarchy of FileUrlGeneratorInterface

All classes that implement FileUrlGeneratorInterface

14 files declare their use of FileUrlGeneratorInterface
AjaxCssForm.php in core/modules/ckeditor/tests/modules/src/Form/AjaxCssForm.php
BaseFieldFileFormatterBase.php in core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php
CKEditor.php in core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
CssCollectionRenderer.php in core/lib/Drupal/Core/Asset/CssCollectionRenderer.php
CssCollectionRendererUnitTest.php in core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php

... See full list

File

core/lib/Drupal/Core/File/FileUrlGeneratorInterface.php, line 24

Namespace

Drupal\Core\File
View source
interface FileUrlGeneratorInterface {

  /**
   * Creates a root-relative web-accessible URL string.
   *
   * @param string $uri
   *   The URI to a file for which we need an external URL, or the path to a
   *   shipped file.
   *
   * @return 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.
   *
   * @throws \Drupal\Core\File\Exception\InvalidStreamWrapperException
   *   If a stream wrapper could not be found to generate an external URL.
   */
  public function generateString(string $uri) : string;

  /**
   * Creates an absolute web-accessible URL string.
   *
   * @param string $uri
   *   The URI to a file for which we need an external URL, or the path to a
   *   shipped file.
   *
   * @return string
   *   An absolute string containing a URL that may be used to access the
   *   file.
   *
   * @throws \Drupal\Core\File\Exception\InvalidStreamWrapperException
   *   If a stream wrapper could not be found to generate an external URL.
   */
  public function generateAbsoluteString(string $uri) : string;

  /**
   * Creates a root-relative web-accessible URL object.
   *
   * @param string $uri
   *   The URI to a file for which we need an external URL, or the path to a
   *   shipped file.
   *
   * @return \Drupal\Core\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.
   *
   * @throws \Drupal\Core\File\Exception\InvalidStreamWrapperException
   *   If a stream wrapper could not be found to generate an external URL.
   */
  public function generate(string $uri) : Url;

  /**
   * 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.
   *
   * @param string $file_url
   *   A file URL of a local file as generated by
   *   \Drupal\Core\File\FileUrlGenerator::generate().
   * @param 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 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.
   */
  public function transformRelative(string $file_url, bool $root_relative = TRUE) : string;

}

Members