diff --git a/src/Plugin/Field/FieldFormatter/FileLinkSeparateFormatter.php b/src/Plugin/Field/FieldFormatter/FileLinkSeparateFormatter.php index 611d8ce86ab8f4562525b611015efd91649a530f..623a947abc7856d40dfb0e44556b41931cb022ad 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 664bae0e235d230249d611fc6e939bd16097b719..a153a761d9dcae71b35e57b0eba2b37b5d8611a5 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 & 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 & 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 & 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 & 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 & 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 & 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 & 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 & 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())); } /**