How to display file size information using media

Created on 28 July 2021, over 3 years ago
Updated 23 September 2023, about 1 year ago

Hi,

I'm using media for my documents and I would like to show a file size information for every document that I'm attaching to site.
In file-link.html.twig <!-- BEGIN OUTPUT from 'themes/contrib/bootstrap_barrio/templates/field/file-link.html.twig' --> I have only:

<span{{ attributes }}>{{ icon }} {{ link }}</span>

How can I render a file size in my subtheme?
Thanks

💬 Support request
Status

Closed: outdated

Version

5.5

Component

Code

Created by

🇵🇱Poland marcjana_84

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 krunalunadkat Pune

    Hi @marcjana_84,

    You can try using below solution,

    1) Add this in .theme file in custom sub theme

    <?php
    function HOOK_preprocess_file_link(&$variables) {
      if (isset($variables['file'])) {
        $variables['fileLinkDisplay'] = [
          'fileSize' => $variables['file']
            ->getSize(),
          'fileSizeFormatted' => format_size($variables['file']
            ->getSize()),
        ];
      }
    }
    ?>

    2) Add this to custom sub theme
    -> themes/custom/THEME_NAME/templates/field/file-link.html.twig

    {{ fileLinkDisplay.fileSize }} and {{ fileLinkDisplay.fileSizeFormatted }} will be available in your template.
    In twig file we can use as per below:-
    <span{{ attributes }}>{{ icon }} {{ link }} {{ fileLinkDisplay.fileSize }}  {{ fileLinkDisplay.fileSizeFormatted }}</span>
    

    Thanks

  • Status changed to Needs review over 1 year ago
  • Status changed to Closed: outdated about 1 year ago
Production build 0.71.5 2024