Problem/Motivation
Depending on how you look at it, this is either an issue with the file module or with views, or potentially both.
You can apply the "File Size" formatter to any integer field in views, but due to the way the isApplicable() function is written, it will only ever apply if the field is named "filesize".
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return parent::isApplicable($field_definition) && $field_definition->getName() === 'filesize';
}
I saw in the original issue #2456709: File views handlers need to be replaced with entity-aware formatters that this was intentional but I wasn't exactly sure why, especially since the formatter is only passing the integer value into format_size().
Similar behavior can be found with the FileExtensionFormatter, FilemimeFormatter and FileUriFormatter. FilemimeFormatter seems to be the only one where it would be absolutely necessary based on how it is presently written.
The fact that views presents "File size" as an option for all integers but it doesn't actually work for all integers creates a confusing user experience.
Steps to Reproduce
- Add a Number (integer) field to a content type.
- Create a content entity of that content type and specify a value for that number field.
- Create a view that looks for that content type and add your new field.
- Specify the "File Size" formatter.
- Look at the view and see it is not formatted
Proposed resolution
Either:
1. Remove the field name requirement.
2. Hide formatters that return FALSE for isApplicable() in views.
Remaining tasks
TBD
User interface changes
TBD
API changes
TBD
Data model changes
TBD
Comment | File | Size | Author |
---|
Comments
Comment #11
quietone CreditAttribution: quietone at PreviousNext commentedI tested on Drupal 10.1.x and the 'file size' formatter is available for integer fields. I selected a thousand marker but it was not displayed when the formatter was 'file size'. In fact, the selection wasn't saved.
Moving to views.