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