I am working in the node template. In my content type I have a referenced field to media called 'field_event'. I created a new media type called 'document'. Inside document there is a file upload field called 'field_document'. I need to print the file name, size and extension all separately in table cells. Example: Myfile | 80kb | pdf

This renders the file, link and name.

{{ content.field_event }}

Gives me: mypdf.pdf 80.00 KB, This is the name of my pdf

This prints out only the name

{{ node.field_event.entity.name.value }}

Gives me: This is the name of my pdf

How can I get the file extension? I have tried these.

{{ node.field_event.document.entity.field_document.entity.filesize.value }}

{{ node.field_event|field_target_entity.field_mime_type.value }}

{{ node.field_event|field_target_entity.field_mime_type.entity.filemime.value }}

I am using twig tweak

1 Answer 1

{% set extension = node.field_event.entity.filemime.value|split('/') %}
{% set extensionDot = '.' ~ extension[1] %}

// File name
{{ node.field_event.entity.filename.value|replace({extensionDot : ''}) }}

// File size
{{ node.field_event.entity.filesize.value / 1000|round }}KB

// File extension
{{ extension[1] }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.