Skip to main content

Since upgrading to Drupal 9.3.0 have you come across this error?

Edit mediaTypeError
: Argument 1 passed to Drupal\Core\File\FileUrlGenerator::generateString() must be of the type string, null given, called in /app/web/sites/default/files/php/twig/61baa150bc5f6_media--document.html.twig_h9bK0We32VfjIKYeac5J1n4Mn/6wcKI-HiereIg8aXE0Znemy4PM0s6woj_Cr2Trl_U68.php on line 59 in
Drupal\Core\File\FileUrlGenerator->generateString()
(line 58 of /app/web/core/lib/Drupal/Core/File/FileUrlGenerator.php).

There is a discussion about this issue on the Drupal site

TypeError: Argument 1 passed to Drupal\Core\File\FileUrlGenerator::generateString() must be of the type string, null given

 

Post upgrading I have had this issue on a couple of sites.  While the patch comes out, in all of my situations the issue was with custom themes in twig files.  Situations such as

{% set url = file_url(document_entity.uri.value ) %}

Almost all of file_url functions were wrapped in an if statement.  This one was not.  So the fix is of course to wrap in an if statement and catch null values.

{% set file_uri_path = document_entity.uri.value %}
{% if file_uri_path is not null %}
    {% set url = file_url(file_uri_path) %}
{% endif %}

 

Yes you can shorten this to become a ternary operator.

 

Function file_create_url() is deprecated - how to call it now

However, in the fore mentioned article on the Drupal site, note that if you have been using 

file_create_url($path);

That this is now deprecated in 9.3.0 and will be removed in 10.0.  It's highly recommend that you begin to prepare your code now for this change by altering the function to 

\Drupal::service('file_url_generator')->generateAbsoluteString($path);

 

 

Related articles

Andrew Fletcher04 Apr 2025
Managing .gitignore changes
When working with Git, the .gitignore file plays a critical role in controlling which files and folders are tracked by version control. Yet, many developers are unsure when changes to .gitignore take effect and how to manage files that are already being tracked. This uncertainty can lead to...
Andrew Fletcher26 Mar 2025
How to fix the ‘Undefined function t’ error in Drupal 10 or 11 code
Upgrading to Drupal 10.4+ you might have noticed a warning in their code editor stating “Undefined function ‘t’”. While Drupal’s `t()` function remains valid in procedural code, some language analysis tools — such as Intelephense — do not automatically recognise Drupal’s global functions. This...
Andrew Fletcher17 Mar 2025
Upgrading to PHP 8.4 challenges with Drupal contrib modules
The upgrade from PHP 8.3.14 to PHP 8.4.4 presents challenges for Drupal 10.4 websites, particularly when dealing with contributed modules. While Drupal core operates seamlessly, various contrib modules have not yet been updated to accommodate changes introduced in PHP 8.4.x. This has resulted in...