From 9b55418bbafbc5c2fe4454e626d5d10f1dedba4c Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Sat, 22 Jul 2023 14:50:00 +0200
Subject: [PATCH] Issue #3261229 by mfb, danflanagan8, smustgrave, daffie,
 catch, alexpott: Passing null to parameter #1 ($num) of type int|float to
 abs() is deprecated

(cherry picked from commit 5f3e8885b8d226736ee4ff6d36843af57ca197dd)
---
 core/modules/file/file.module                          |  2 +-
 .../src/Plugin/Field/FieldFormatter/TableFormatter.php |  2 +-
 .../file/tests/src/Functional/FileFieldDisplayTest.php | 10 ++++++++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 215d12a7ceb7..848497d93287 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -1071,7 +1071,7 @@ function template_preprocess_file_link(&$variables) {
   // Set file classes to the options array.
   $variables['attributes'] = new Attribute($variables['attributes']);
   $variables['attributes']->addClass($classes);
-  $variables['file_size'] = format_size($file->getSize() ?? 0);
+  $variables['file_size'] = $file->getSize() !== NULL ? format_size($file->getSize()) : '';
 
   $variables['link'] = Link::fromTextAndUrl($link_text, $url->mergeOptions($options))->toRenderable();
 }
diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
index 3c0aa914fabf..e94156a6d365 100644
--- a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
+++ b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php
@@ -39,7 +39,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
               ],
             ],
           ],
-          ['data' => format_size($file->getSize())],
+          ['data' => $file->getSize() !== NULL ? format_size($file->getSize()) : $this->t('Unknown')],
         ];
       }
 
diff --git a/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php
index 3e342dae4ce9..844df162bf34 100644
--- a/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php
+++ b/core/modules/file/tests/src/Functional/FileFieldDisplayTest.php
@@ -239,6 +239,16 @@ public function testDescriptionDefaultFileFieldDisplay() {
 
     $this->drupalGet('node/' . $nid);
     $this->assertSession()->elementTextContains('xpath', '//a[@href="' . $node->{$field_name}->entity->createFileUrl() . '"]', $description);
+
+    // Test that null file size is rendered as "Unknown".
+    $nonexistent_file = File::create([
+      'uri' => 'temporary://' . $this->randomMachineName() . '.txt',
+    ]);
+    $nonexistent_file->save();
+    $node->set($field_name, $nonexistent_file->id());
+    $node->save();
+    $this->drupalGet('node/' . $nid);
+    $this->assertSession()->elementTextEquals('xpath', '//a[@href="' . $node->{$field_name}->entity->createFileUrl() . '"]/../../../td[2]', 'Unknown');
   }
 
 }
-- 
GitLab