Loading core/modules/file/src/Hook/FileThemeHooks.php +3 −1 Original line number Diff line number Diff line Loading @@ -39,6 +39,7 @@ public function theme(): array { 'file' => NULL, 'description' => NULL, 'attributes' => [], 'with_size' => TRUE, ], 'initial preprocess' => static::class . ':preprocessFileLink', ], Loading Loading @@ -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']; Loading Loading @@ -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(); } Loading core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php +2 −0 Original line number Diff line number Diff line Loading @@ -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(), ], Loading core/modules/file/tests/src/Functional/FileFieldDisplayTest.php +4 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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([ Loading Loading
core/modules/file/src/Hook/FileThemeHooks.php +3 −1 Original line number Diff line number Diff line Loading @@ -39,6 +39,7 @@ public function theme(): array { 'file' => NULL, 'description' => NULL, 'attributes' => [], 'with_size' => TRUE, ], 'initial preprocess' => static::class . ':preprocessFileLink', ], Loading Loading @@ -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']; Loading Loading @@ -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(); } Loading
core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php +2 −0 Original line number Diff line number Diff line Loading @@ -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(), ], Loading
core/modules/file/tests/src/Functional/FileFieldDisplayTest.php +4 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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([ Loading