Remove hardcoded restriction of filesize formatter to fields named "filesize"

Created on 6 December 2017, almost 7 years ago
Updated 27 March 2024, 8 months ago

Problem/Motivation

Drupal's "File Size" views formatter (\Drupal\file\Plugin\Field\FieldFormatter\FileSize) has a hardcoded behaviour that it can only display field definition where the field name is "filesize". This obstructs the formatter being used for file size fields attached to a Media object definition.

This is important for media metadata extraction, see #2928798: Add extractors: mimetype & filesize โ†’ . It also permits this useful formatter being used in other contexts where there is a bytesize measurement stored in a Drupal field.

Steps to reproduce

  • Create a new Drupal site
  • Enable Media, Media Library
  • At /admin/structure/media/manage/image/fields/add-field configure a field for storage of filesize. The name isn't significant; the default machine name will be prefixed with field_ which is sufficient to demonstrate this issue.
  • At /admin/structure/media/manage/image under Field Mapping, map "File size" to your new field then press Save.
  • At /admin/content/media/image add a new image. Do not input a value to the filesize field.
  • Post-upload, you should see your image listed at /admin/content/media. Click back to the image to verify that the filesize field was correctly populated by Media.
  • At /admin/structure/views/view/media/edit/media_page_list add a Field to the view to display the file size field.
  • On "Configure field: Media: ", select "File size" for "Formatter"
  • The Views UI behaves inconsistently at this point. While it will preserve the value of Formatter selection, Formatter settings will not be preserved; if you change "Thousand marker" to "comma", it will be "- None -" when revisisted. (NB: Thousand marker is likely only visible if the underlying storage is numeric.)
  • The view will continue to display the filesize value as though the Formatter was set to "Unformatted"; no file size formatting is applied.

Proposed resolution

Allow this formatter for all fields of type "integer". The integer constraint is applied via existing annotation.

Remaining tasks

- Review
- Merge!

User interface changes

  1. The "File size" formatter will be re-labelled "Byte size" to consider other usages (eg fields representing length of an encoded value, or amount of data consumed in downloads)
  2. "Byte size" formatter will become available for fields of type "integer".

API changes

None

Data model changes

None

โœจ Feature request
Status

Fixed

Version

10.3 โœจ

Component
File moduleย  โ†’

Last updated 5 days ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany geek-merlin Freiburg, Germany

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Rinku Jacob 13 Kerala

    Re-rolled patch #29 for drupal version 10.1.x-dev. Please Review.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia gauravvvv Delhi, India

    Updated attributions.

  • Status changed to Needs work almost 2 years ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand xurizaemon ลŒtepoti, Aotearoa ๐Ÿ

    Updated description to incorporate rename.

  • Status changed to Needs review almost 2 years ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand xurizaemon ลŒtepoti, Aotearoa ๐Ÿ

    I'm setting this back to Needs Review. It was set to "Needs work" by the patch in #32 triggering tests which failed, but the patch doesn't introduce any meaningful change that I can see, and the tests pass once more now that the MR is refreshed.

    The only difference I observe between patches in #29 and #32 is that Git's context lines change, with no change in the patch outcome.

    @Rinku Jacob 13 if your patch was intended to change something but did not, or introduced some meaningful change that I'm missing, please respond to clarify so we don't lose something here. The issue & MR did need a bump so I'm grateful for that, just want to check if there's a detail missing here. Thanks!

    Unsetting patch visibility as the MR is up to date once more.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    I think we should deprecate this function vs just fully remove. Has it been checked if any contrib modules could be using it?

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany hctom

    @smustgrave: can you explain why this method should be deprecated? It is defined in the base class and keeping it in this formatter class won't make any sense though.

  • last update about 1 year ago
    30,136 pass
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand xurizaemon ลŒtepoti, Aotearoa ๐Ÿ

    Given that the presence/only functionality of the function is the bug described here (preventing the formatter being used on other fields unless the field name condition is met), I don't see how deprecating it improves things - would it not just lock the existing behaviour in for an additional release?

    Removing the function seems simplest, but if you feel it's better this MR made the function a no-op in case something is using it, I am happy to update the patch accordingly.

  • First commit to issue fork.
  • Pipeline finished with Success
    12 months ago
    Total: 579s
    #66066
  • Status changed to Needs review 12 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand ericgsmith

    Rebased and adding patch of MR at e2833e38 for those needing a stable patch to apply via composer patches.

    Setting this back to needs review - I think the needs work comment has been addressed via the label change, deprecation makes no sense here.

    What is probably worth reviewing is if the formatter itself should be deprecated in favour of a new formatter of id byte_size introduced somewhere other than the file module?

    As mentioned in #9 this would now be applicable to all integer fields (hence the subsequent label change) - but since then ๐Ÿ“Œ Deprecate format_size() and use Drupal\Core\StringTranslation\ByteSizeMarkup instead Fixed has been committed which as far as I can see means that with the proposed change here there is nothing specifically file related in this formatter anymore. Does it make sense that file module would provide this formatter?

  • Status changed to Needs work 11 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States benjifisher Boston area

    Usability review

    We discussed this issue at ๐Ÿ“Œ Drupal Usability Meeting 2024-01-12 Active . That issue has a link to a recording of the meeting.

    For the record, the attendees at the usability meeting were @benjifisher, @rkoller, @shaal, and @simohell.

    It is certainly an improvement that the formatter no longer displays the ignored "Thousand marker" option. Looking at the patch, I am not sure why this changes.

    It is hard to think of a way to describe the formatter. The current label "Byte size" is consistent with the internal name of the ByteSizeMarkup class, but we felt that it is not very clear in the context of the admin UI. We looked at https://en.wikipedia.org/wiki/File_size and https://en.wikipedia.org/wiki/Units_of_information for inspiration, and did not find anything helpful. (Neither "systematic multiples" nor "with binary prefix" seems clear.)

    The best suggestion we could think of was "Bytes (KB, MB, ...)". We think that gives a clear indication of what to expect. I am setting the status to NW for this change.

    I notice that Comment #26 suggests that parentheses (or brackets) in option labels should be avoided. But there are places in the admin UI where parentheses are used. For example, when I add a filter to a View, two of the options for the operator are "Is empty (NULL)" and "Is not empty (NOT NULL)". I also see the logic in "human-readable": it would make sense to anyone familiar with the ls -h command.

    It is out of scope for this issue, but we felt that the formatter should offer more options. For example, there should be options for the decimal marker and whether to use "KB" or "KiB" or "kB". Maybe there should be an option to use powers of 10 (SI notation) instead of powers of 1024, or maybe that should be a separate formatter.

    I see some discussion of moving the formatter out of the file module. Perhaps it makes sense to do that in a follow-up issue that also makes the formatter more configurable.

    If you want more feedback from the usability team, a good way to reach out is in the #ux channel in Slack.

  • Pipeline finished with Success
    9 months ago
    Total: 552s
    #113394
  • Status changed to Needs review 9 months ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand ericgsmith

    Thanks @benjifisher for looking at this!

    It is certainly an improvement that the formatter no longer displays the ignored "Thousand marker" option

    I'm not sure I understand this - can you please clarify what the change is here you saw? It doesn't sound like something this patch changes but I may be missing something.

    I have rebased the MR and applied the label change suggested from #44.

    I have also created a draft change record https://www.drupal.org/node/3426241 โ†’ since this is a UI change for site builders.

    Back to needs review.

  • Status changed to RTBC 9 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Opened a follow up for the discussion of moving the formatter ๐Ÿ“Œ Discuss moving formatter from file module Active

    Change looks good, and not sure removing the function requires test coverage.

    • longwave โ†’ committed 1bdd9d75 on 10.3.x
      Issue #2928801 by xurizaemon, ericgsmith, geek-merlin, RoSk0, Gauravvvv...
  • Status changed to Fixed 9 months ago
    • longwave โ†’ committed e97d6c18 on 11.x
      Issue #2928801 by xurizaemon, ericgsmith, geek-merlin, RoSk0, Gauravvvv...
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom longwave UK

    Agree that naming this is awkward, but the suggested version does appear to make more sense when shown in a list of similar formatters.

    Committed and pushed e97d6c18f4 to 11.x and 1bdd9d754b to 10.3.x. Thanks!

    Also published the change record.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States benjifisher Boston area

    @ericgsmith:

    It is almost 3 months since I looked at this issue. I think I tested manually, but I may have been relying on the issue summary when I mentioned the "Thousand marker" option.

    The Views UI behaves inconsistently at this point. While it will preserve the value of Formatter selection, Formatter settings will not be preserved; if you change "Thousand marker" to "comma", it will be "- None -" when revisisted. (NB: Thousand marker is likely only visible if the underlying storage is numeric.)

    I also wrote, "Looking at the patch, I am not sure why this changes." So probably I did test manually. Perhaps that changed because of some other issue, not this one.

    If anyone really cares, the recording of the usability meeting is available: see the link in Comment #44.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024