Commit ddf5fc87 authored by catch's avatar catch
Browse files

Issue #3254245 by kim.pepper, Jeya sundhar, mrweiner, bakulahluwalia, Berdir,...

Issue #3254245 by kim.pepper, Jeya sundhar, mrweiner, bakulahluwalia, Berdir, catch, newaytech, Summit: TypeError: Argument 1 passed to Drupal\Core\File\FileUrlGenerator::generateString() must be of the type string, null given

(cherry picked from commit 9d3bdf2b)
parent 802ec71e
......@@ -65,6 +65,9 @@
*/
function file_create_url($uri) {
@trigger_error('file_create_url() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use the appropriate method on \Drupal\Core\File\FileUrlGeneratorInterface instead. See https://www.drupal.org/node/2940031', E_USER_DEPRECATED);
if (is_null($uri)) {
return NULL;
}
try {
return \Drupal::service('file_url_generator')->generateAbsoluteString($uri);
}
......
......@@ -107,7 +107,7 @@ public function getFunctions() {
new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
new TwigFunction('link', [$this, 'getLink']),
new TwigFunction('file_url', [$this->fileUrlGenerator, 'generateString']),
new TwigFunction('file_url', [$this, 'getFileUrl']),
new TwigFunction('attach_library', [$this, 'attachLibrary']),
new TwigFunction('active_theme_path', [$this, 'getActiveThemePath']),
new TwigFunction('active_theme', [$this, 'getActiveTheme']),
......@@ -272,6 +272,22 @@ public function getLink($text, $url, $attributes = []) {
return $build;
}
/**
* Gets the file URL.
*
* @param string|null $uri
* The file URI.
*
* @return string
* The file URL.
*/
public function getFileUrl(?string $uri) {
if (is_null($uri)) {
return NULL;
}
return $this->fileUrlGenerator->generateString($uri);
}
/**
* Gets the name of the active theme.
*
......
<div>file_url: {{ file_url('core/modules/system/tests/modules/twig_theme_test/twig_theme_test.js') }}</div>
<div>file_url: {{ file_url(undefined_variable) }}</div>
......@@ -21,6 +21,7 @@ public function testDeprecatedFileCreateUrl() {
$filepath = 'core/assets/vendor/jquery/jquery.min.js';
$url = file_url_transform_relative(file_create_url($filepath));
$this->assertNotEmpty($url);
$this->assertEquals(NULL, file_create_url(NULL));
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment