Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
\Drupal\file\Entity\File::getSize()
can cause deprecations on PHP 8.1 because we pass the NULL value into format_size()
in template_preprocess_file_link()
Steps to reproduce
Run \Drupal\Tests\file\Kernel\FileItemTest
Proposed resolution
Either make \Drupal\file\Entity\File::getSize() conform to its interface - it says it returns a string or fix template_preprocess_file_link()
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#2 | 3240220-2.patch | 658 bytes | alexpott |
Comments
Comment #2
alexpottHere's the fix from the meta.
Comment #3
daffie CreditAttribution: daffie commentedCan we update the docblock of the function
format_size()
that the first parameter must be a int or a float. Can we also do a check for that and throw a deprecation warning when this is not the case. See: https://www.php.net/manual/en/function.abs.phpComment #4
alexpott@daffie it can be a string too... which it nearly always is... see https://3v4l.org/ejYM6
Comment #5
alexpottI think the question here is not about format_size() but about whether the fix should be in \Drupal\file\Entity\File::getSize() or template_preprocess_file_link()
Comment #6
daffie CreditAttribution: daffie commentedFrom core/includes/common.inc:
and PHP defines the functions
abs()
asabs(int|float $num): int|float
. Therefor the variable$size
must be an integer value or a float value. It is not saying anything about allowing string values.Comment #7
alexpott@daffie see https://3v4l.org/ejYM6 - not all PHP docs are correct.
Comment #8
andypostI bet it because PHP using internal conversion from strings to numbers, I bet because https://wiki.php.net/rfc/string_to_number_comparison
No need to fix docs for function which is going to be deprecated (naming question) #2157945: Deprecate format_size() and use Drupal\Core\StringTranslation\ByteSizeMarkup instead
The issue fixes the problem as #7 points with BC so no CR needed
Comment #9
larowlanCommitted 01a737b and pushed to 9.3.x. Thanks!