diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 215d12a7ceb79170f13ccfbdc926bda94406679a..848497d932876aebbc9cf27788278da1d43f775e 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 3c0aa914fabf65298c10b6ef16189c1ab5a66a13..e94156a6d36526b0680a3f7848f2453394fabf0d 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 3e342dae4ce90f28ae2c186480fa056b90888e50..844df162bf346d59664e8c17da55d3bd3e96403f 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'); } }