Reply to comment

Drupal 8 Media: Format file size output / display

If you'd like to output Drupal 8 Media file size (mapper field) output in human-readable format, this snippet may help you!

in MYTHEME.theme file:

  1. function MYTHEME_preprocess_media(&$variables) {
  2.   /** @var \Drupal\media_entity\MediaInterface $media */
  3.   $media = $variables['elements']['#media'];
  4.  
  5.   if ($media->bundle() == 'document' && !empty($media-> field_document_size)) {
  6.     $variables['content']['field_document_size'][0]['#markup'] = format_size(\Drupal\Component\Utility\Bytes::toInt($variables['content']['field_document_size'][0]['#markup']));
  7.   }
  8. }

of course you can also use it similar in your custom module.

Reply

The content of this field is kept private and will not be shown publicly.

More information about formatting options