file-link template should not always display file_size

Created on 3 March 2020, about 5 years ago
Updated 12 February 2025, 3 months ago

Problem/Motivation

Discovered in #3117217: Decouple core theme dependency on functions in stable.theme
Claro's file-link template always displays file_size, but this should not always be displayed
<span{{ attributes }}>{{ link }} <span class="file__size">({{ file_size }})</span></span>

When file-link used by image_widget, file_size should always be displayed, but there are other uses for this template such as by the file_table formatter, which displays file size in a separate column.

Currently, the file size is added to file-link used by image_widget via stable_preprocess_image_widget().
Adding this markup in a #suffix is not ideal.

function stable_preprocess_image_widget(&$variables) {
  if (!empty($variables['element']['fids']['#value'])) {
    $file = reset($variables['element']['#files']);
    $variables['data']['file_' . $file->id()]['filename']['#suffix'] = ' <span class="file-size">(' . format_size($file->getSize()) . ')</span> ';
  }
}

(In the near future Drupal 9 core themes will add their own preprocess_image_widget(), and there will be @todo's pointing to this issue.)

Unless there's no possible way to do it otherwise, the solution for Claro should not add this markup in a #suffix. If a solution is found that does not use #suffix, this issue can be re-scoped and the solution can be applied to all themes and possibly the core file-link template .

Proposed resolution

Find a way to target Claro's file-link templates within image widgets so they display file_size. If this can be achieved without #suffix, apply this to all core themes and remove the preprocess functions with @todo items pointing to this issue.

If the solution can't be applied to all core themes, create a followup to find a non-#suffix solution and change the @todo items to point to that.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

Claro theme

Created by

🇺🇸United States bnjmnm Ann Arbor, MI

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India niranjan_panem Gurugram

    In drupal 11 claro theme suffix for file-size is not added in preprocess_image_widget. Below is the screen shot of it.

  • 🇺🇸United States bnjmnm Ann Arbor, MI

    @niranjan_panem that is correct because, as the issue summary states Claro's file-link template always displays file_size

    The template is where the issue lies

    The preprocess in the issue summary was explaining how other themes addressed this in the past.

  • 🇺🇸United States dafeder Philadelphia, PA

    What is the argument against wrapping the whole span in a conditional to check if file_size is set? This is how it is handled in the core twig template for file module. There are other contexts when that variable might be empty, and you wouldn't want to show the parentheses if there's nothing between them. Likewise, if you want to hide it you could just unset that variable via preprocess.

Production build 0.71.5 2024