You are here

function file_url_transform_relative in Drupal 8

Same name and namespace in other branches
  1. 9 core/includes/file.inc \file_url_transform_relative()

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 file_create_url().

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.

See also

file_create_url()

Related topics

58 calls to file_url_transform_relative()
AjaxCssForm::generateResponse in core/modules/ckeditor/tests/modules/src/Form/AjaxCssForm.php
Generates an AJAX response to add CSS to a CKEditor Text Editor instance.
AttachedAssetsTest::testAddFiles in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests adding a CSS and a JavaScript file.
AttachedAssetsTest::testAddJsFileWithQueryString in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests JavaScript files that have querystrings attached get added right.
AttachedAssetsTest::testAggregatedAttributes in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests that attributes are maintained when JS aggregation is enabled.
AttachedAssetsTest::testAttributes in core/tests/Drupal/KernelTests/Core/Asset/AttachedAssetsTest.php
Tests adding JavaScript files with additional attributes.

... See full list

1 string reference to 'file_url_transform_relative'
CKEditor::buildContentsCssJSSetting in core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
Builds the "contentsCss" configuration part of the CKEditor JS settings.

File

core/includes/file.inc, line 281
API for handling file uploads and server file management.

Code

function file_url_transform_relative($file_url) {

  // Unfortunately, we pretty much have to duplicate Symfony's
  // Request::getHttpHost() method because Request::getPort() may return NULL
  // instead of a port number.
  $request = \Drupal::request();
  $host = $request
    ->getHost();
  $scheme = $request
    ->getScheme();
  $port = $request
    ->getPort() ?: 80;
  if ('http' == $scheme && $port == 80 || 'https' == $scheme && $port == 443) {
    $http_host = $host;
  }
  else {
    $http_host = $host . ':' . $port;
  }
  return preg_replace('|^https?://' . preg_quote($http_host, '|') . '|', '', $file_url);
}