From bf47b9efaeddb54e7cd0270cdd5cd091ae2fc611 Mon Sep 17 00:00:00 2001
From: Adam Nagy <joevagyok@gmail.com>
Date: Mon, 10 Mar 2025 11:36:54 +0100
Subject: [PATCH] Issue #3511960: Fix type error when size is null in
 formatter.

---
 .../FileLinkSeparateFormatter.php             |  6 ++----
 tests/src/Kernel/FileLinkFormatterTest.php    | 20 +++++++++++++++++--
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/Plugin/Field/FieldFormatter/FileLinkSeparateFormatter.php b/src/Plugin/Field/FieldFormatter/FileLinkSeparateFormatter.php
index 611d8ce..623a947 100644
--- a/src/Plugin/Field/FieldFormatter/FileLinkSeparateFormatter.php
+++ b/src/Plugin/Field/FieldFormatter/FileLinkSeparateFormatter.php
@@ -30,10 +30,8 @@ class FileLinkSeparateFormatter extends LinkSeparateFormatter {
     foreach ($items as $delta => $item) {
       if (isset($element[$delta])) {
         $element[$delta]['#theme'] = 'file_link_formatter_link_separate';
-        $element[$delta] += [
-          '#size' => $this->getSetting('format_size') ? ByteSizeMarkup::create($item->size) : $item->size,
-          '#format' => $item->format,
-        ];
+        $element[$delta]['#size'] = $this->getSetting('format_size') ? ByteSizeMarkup::create($item->size ?? 0) : $item->size;
+        $element[$delta]['#format'] = $item->format;
       }
     }
     return $element;
diff --git a/tests/src/Kernel/FileLinkFormatterTest.php b/tests/src/Kernel/FileLinkFormatterTest.php
index 664bae0..a153a76 100644
--- a/tests/src/Kernel/FileLinkFormatterTest.php
+++ b/tests/src/Kernel/FileLinkFormatterTest.php
@@ -64,6 +64,10 @@ class FileLinkFormatterTest extends KernelTestBase {
         'status' => 200,
         'headers' => ['Content-Length' => 476],
       ],
+      'http://file_link.drupal/file-no-size.txt' => [
+        'status' => 200,
+        'headers' => ['Content-Type' => 'text/plain'],
+      ],
     ];
 
     new Settings($settings);
@@ -87,6 +91,10 @@ class FileLinkFormatterTest extends KernelTestBase {
           'uri' => 'http://file_link.drupal/file-no-type.md',
           'title' => 'A file with no type.',
         ],
+        [
+          'uri' => 'http://file_link.drupal/file-no-size.txt',
+          'title' => 'A file with no size.',
+        ],
       ],
     ]);
     $this->entity->save();
@@ -102,12 +110,13 @@ class FileLinkFormatterTest extends KernelTestBase {
     ];
     $this->renderEntityField($display_options);
     $items = $this->cssSelect('main > div > div');
-    $this->assertCount(4, $items);
+    $this->assertCount(5, $items);
 
     $this->assertEquals('<div><a href="http://file_link.drupal/fancy-file-1.txt">http://file_link.drupal/fancy-file-1.txt</a> (text/plain, 27 bytes)</div>', $this->normaliseSpaces((string) $items[0]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/fancy-file-2.md">A very long &amp; strange example title that could break the nice layout of the sit…</a> (text/markdown, 40.8 MB)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/broken-file-response.txt">A file with no type nor size.</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/file-no-type.md">A file with no type.</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div><a href="http://file_link.drupal/file-no-size.txt">A file with no size.</a></div>', $this->normaliseSpaces((string) $items[4]->asXML()));
 
     $display_options['settings'] = [
       'format_size' => FALSE,
@@ -118,6 +127,7 @@ class FileLinkFormatterTest extends KernelTestBase {
     $this->assertEquals('<div><a href="http://file_link.drupal/fancy-file-2.md">A very long &amp; strange example title that could break the nice layout of the sit…</a> (text/markdown, 42784327)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/broken-file-response.txt">A file with no type nor size.</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/file-no-type.md">A file with no type.</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div><a href="http://file_link.drupal/file-no-size.txt">A file with no size.</a></div>', $this->normaliseSpaces((string) $items[4]->asXML()));
 
     $display_options['settings'] = [
       'trim_length' => 22,
@@ -128,6 +138,7 @@ class FileLinkFormatterTest extends KernelTestBase {
     $this->assertEquals('<div><a href="http://file_link.drupal/fancy-file-2.md">A very long &amp; strange…</a> (text/markdown, 40.8 MB)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/broken-file-response.txt">A file with no type n…</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/file-no-type.md">A file with no type.</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div><a href="http://file_link.drupal/file-no-size.txt">A file with no size.</a></div>', $this->normaliseSpaces((string) $items[4]->asXML()));
 
     $display_options['settings'] = [
       'trim_length' => NULL,
@@ -138,6 +149,7 @@ class FileLinkFormatterTest extends KernelTestBase {
     $this->assertEquals('<div><a href="http://file_link.drupal/fancy-file-2.md">A very long &amp; strange example title that could break the nice layout of the site.</a> (text/markdown, 40.8 MB)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/broken-file-response.txt">A file with no type nor size.</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div><a href="http://file_link.drupal/file-no-type.md">A file with no type.</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div><a href="http://file_link.drupal/file-no-size.txt">A file with no size.</a></div>', $this->normaliseSpaces((string) $items[4]->asXML()));
   }
 
   /**
@@ -150,12 +162,13 @@ class FileLinkFormatterTest extends KernelTestBase {
     ];
     $this->renderEntityField($display_options);
     $items = $this->cssSelect('main > div > div');
-    $this->assertCount(4, $items);
+    $this->assertCount(5, $items);
 
     $this->assertEquals('<div><a href="http://file_link.drupal/fancy-file-1.txt">http://file_link.drupal/fancy-file-1.txt</a> (text/plain, 27 bytes)</div>', $this->normaliseSpaces((string) $items[0]->asXML()));
     $this->assertEquals('<div>A very long &amp; strange example title that could break the nice layout of the sit… <a href="http://file_link.drupal/fancy-file-2.md">http://file_link.drupal/fancy-file-2.md</a> (text/markdown, 40.8 MB)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div>A file with no type nor size. <a href="http://file_link.drupal/broken-file-response.txt">http://file_link.drupal/broken-file-response.txt</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div>A file with no type. <a href="http://file_link.drupal/file-no-type.md">http://file_link.drupal/file-no-type.md</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div>A file with no size. <a href="http://file_link.drupal/file-no-size.txt">http://file_link.drupal/file-no-size.txt</a> (text/plain, 0 bytes)</div>', $this->normaliseSpaces((string) $items[4]->asXML()));
 
     $display_options['settings'] = [
       'format_size' => FALSE,
@@ -166,6 +179,7 @@ class FileLinkFormatterTest extends KernelTestBase {
     $this->assertEquals('<div>A very long &amp; strange example title that could break the nice layout of the sit… <a href="http://file_link.drupal/fancy-file-2.md">http://file_link.drupal/fancy-file-2.md</a> (text/markdown, 42784327)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div>A file with no type nor size. <a href="http://file_link.drupal/broken-file-response.txt">http://file_link.drupal/broken-file-response.txt</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div>A file with no type. <a href="http://file_link.drupal/file-no-type.md">http://file_link.drupal/file-no-type.md</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div>A file with no size. <a href="http://file_link.drupal/file-no-size.txt">http://file_link.drupal/file-no-size.txt</a></div>', $this->normaliseSpaces((string) $items[4]->asXML()));
 
     $display_options['settings'] = [
       'trim_length' => 22,
@@ -176,6 +190,7 @@ class FileLinkFormatterTest extends KernelTestBase {
     $this->assertEquals('<div>A very long &amp; strange… <a href="http://file_link.drupal/fancy-file-2.md">http://file_link.drup…</a> (text/markdown, 40.8 MB)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div>A file with no type n… <a href="http://file_link.drupal/broken-file-response.txt">http://file_link.drup…</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div>A file with no type. <a href="http://file_link.drupal/file-no-type.md">http://file_link.drup…</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div>A file with no size. <a href="http://file_link.drupal/file-no-size.txt">http://file_link.drup…</a> (text/plain, 0 bytes)</div>', $this->normaliseSpaces((string) $items[4]->asXML()));
 
     $display_options['settings'] = [
       'trim_length' => NULL,
@@ -186,6 +201,7 @@ class FileLinkFormatterTest extends KernelTestBase {
     $this->assertEquals('<div>A very long &amp; strange example title that could break the nice layout of the site. <a href="http://file_link.drupal/fancy-file-2.md">http://file_link.drupal/fancy-file-2.md</a> (text/markdown, 40.8 MB)</div>', $this->normaliseSpaces((string) $items[1]->asXML()));
     $this->assertEquals('<div>A file with no type nor size. <a href="http://file_link.drupal/broken-file-response.txt">http://file_link.drupal/broken-file-response.txt</a></div>', $this->normaliseSpaces((string) $items[2]->asXML()));
     $this->assertEquals('<div>A file with no type. <a href="http://file_link.drupal/file-no-type.md">http://file_link.drupal/file-no-type.md</a></div>', $this->normaliseSpaces((string) $items[3]->asXML()));
+    $this->assertEquals('<div>A file with no size. <a href="http://file_link.drupal/file-no-size.txt">http://file_link.drupal/file-no-size.txt</a> (text/plain, 0 bytes)</div>', $this->normaliseSpaces((string) $items[4]->asXML()));
   }
 
   /**
-- 
GitLab