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.

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Weitere Informationen über Formatierungsoptionen