8 – Show the file size next to the file link

Here is a pretreatment approach that can work for you.

/ **
* Implements HOOK_preprocess_field ().
*
* @inheritdoc
* /
function HOOK_preprocess_field (& $ variables, $ hook) {
$ element = $ variables['element'];
// Check that field_name is set.
if (isset ($ element['#field_name'])) {
// Check for zone_name, but could check zone_type.
if ($ element['#field_name'] == & # 39; zone_name & # 39;) {
// Set the size of our file to a variable.
$ fileSize = $ element[0]['#file']-> file size -> value;
// Edit our title for the field view.
$ element['#title'] = $ element['#title'] . & # 39; & # 39; $ fileSize;
echo $ element['#title'];
}
}
}

And to set as variable to print in Twig:

function HOOK_preprocess_field (& $ variables, $ hook) {
$ element = $ variables['element'];
// Check that field_name is set.
if (isset ($ element['#field_name'])) {
// Check for zone_name, but could check zone_type.
if ($ element['#field_name'] == & # 39; zone_name & # 39;) {
// Set the size of our file as a variable for Twig.
$ variables['fileSize'] = $ element[0]['#file']-> file size -> value;
}
}
}

Then printed Twig {{ file size }} next to your title / label.

* Note, there may be an API method to get the file size, I have not looked at it long.