Commit ddab8760 authored by catch's avatar catch
Browse files

fix: #3482024 'file_table' formatter shows file size twice

By: @marc.bau
By: @mfb
By: @smustgrave
(cherry picked from commit 687501c3)
parent a8f480f0
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ public function theme(): array {
          'file' => NULL,
          'description' => NULL,
          'attributes' => [],
          'with_size' => TRUE,
        ],
        'initial preprocess' => static::class . ':preprocessFileLink',
      ],
@@ -86,6 +87,7 @@ public function theme(): array {
   *   - description: A description to be displayed instead of the filename.
   *   - attributes: An associative array of attributes to be placed in the a
   *     tag.
   *   - with_size: A Boolean indicating if the file size should be displayed.
   */
  public function preprocessFileLink(array &$variables): void {
    $file = $variables['file'];
@@ -117,7 +119,7 @@ public function preprocessFileLink(array &$variables): void {
    // Set file classes to the options array.
    $variables['attributes'] = new Attribute($variables['attributes']);
    $variables['attributes']->addClass($classes);
    $variables['file_size'] = $file->getSize() !== NULL ? ByteSizeMarkup::create($file->getSize()) : '';
    $variables['file_size'] = ($variables['with_size'] ?? TRUE) && $file->getSize() !== NULL ? ByteSizeMarkup::create($file->getSize()) : '';

    $variables['link'] = (new Link($link_text, $url->mergeOptions($options)))->toRenderable();
  }
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
              '#theme' => 'file_link',
              '#file' => $file,
              '#description' => $this->getSetting('use_description_as_link_text') ? $item->description : NULL,
              // File size has its own column, so do not add it to the link.
              '#with_size' => FALSE,
              '#cache' => [
                'tags' => $file->getCacheTags(),
              ],
+4 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Tests\file\Functional;

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\ByteSizeMarkup;
use Drupal\file\Entity\File;
use Drupal\node\Entity\Node;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
@@ -236,6 +237,9 @@ public function testDescriptionDefaultFileFieldDisplay(): void {

    $this->drupalGet('node/' . $nid);
    $this->assertSession()->elementTextContains('xpath', '//a[@href="' . $node->{$field_name}->entity->createFileUrl() . '"]', $description);
    // Test that setting '#with_size' to FALSE prevents the size from being
    // displayed as part of the file link.
    $this->assertSession()->pageTextContainsOnce((string) ByteSizeMarkup::create($test_file->getSize()));

    // Test that null file size is rendered as "Unknown".
    $nonexistent_file = File::create([