Question

Is there a way to display the file's size next to it in a View? This is for a File field in Drupal 8.

I tried the advice (for D7) here, but I received this error.

The handler for this item is broken or missing. The following details are available: Enabling the appropriate module may solve this issue. Otherwise, check to see if there is a module update available.

I am not sure which module it means. When I go to the modules list, the only thing with "file" in it is the actual File field module, which is installed. I ran composer update earlier today, and the status report shows that everything is current.

I also thought I might be able to do this by adding a field or doing a field re-write. So I looked under the list of available fields to add to the View and didn't see anything about file size there.

Was it helpful?

Solution

Various file properties are available directly in Views if you're Views type is File.

Since your Views type is probably Content you have to add the Relationship for the file field in your content that references the file(s).

The File field inside a Content Type only stores the reference to the file ID, nothing else.

When you add the Relationship for the File field from the Content Category, new fields will become available in Views. I twill be called something like File (field_file:target_id). You can Require it if you only want to see results where that field isn't empty.

When you go to add a new field you will see File size available from the File Category.

OTHER TIPS

You have different options:

  • create a pseudo field which markup is showing file size
  • preprocess the file field
  • relationship on view for file field and then you are able to place filesize field or in overridden markup

If you go with custom you can use (load file entity) function https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/format_size/8.2.x

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top