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 9.3.x 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

Issue fork drupal-2928801

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

axel.rutz created an issue. See original summary.

geek-merlin’s picture

Status: Active » Needs review
FileSize
1.06 KB

Trivial patch flying in, untested yet.

geek-merlin’s picture

Works like a charm here.

geek-merlin’s picture

Issue summary: View changes
geek-merlin’s picture

Issue summary: View changes

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

geek-merlin’s picture

Title: Allow filesize formatter for all » Remove hardcoded restriction of filesize formatter to fields named "filesize"
borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Looks simple enough. I agree that this is unneededly limited.

Berdir’s picture

I can see that this could be useful for other cases, but I think it should be reviewed by the usability team. This exposes a new formatter option for *all* integer fields and for most, it won't make sense, especially not as "File size".

One option could be to change the label.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs usability review

Knocking back to needs review for #9.

hctom’s picture

I'd also agree to remove the restriction, because not being able to use the formatter is for example a problem if you use the "Filesize" field mapping available with the "Image" media source. While you'd use a custom integer field named field_* for that, you won't be able to use the filesize formatter due to the naming restriction.

But I also see the point Berdir mentioned... perhaps some other label would really be more appropriate for this formatter.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xurizaemon made their first commit to this issue’s fork.

xurizaemon’s picture

Issue summary: View changes

xurizaemon’s picture

Issue summary: View changes

For clarity:

- MR https://git.drupalcode.org/project/drupal/-/merge_requests/1196 proposes the bugfix only
- MR https://git.drupalcode.org/project/drupal/-/merge_requests/1197 proposes both the bugfix *and* re-labels the Formatter from "File size" to "Bytesize (human-readable)" based on @berdir's comment in #9

xurizaemon’s picture

Issue summary: View changes

Gauravmahlawat made their first commit to this issue’s fork.

RoSk0 made their first commit to this issue’s fork.

RoSk0’s picture

Changed formatter label to be consistent with others - capitalised and removed brackets:

- *   label = @Translation("byte size (human-readable)"),
+ *   label = @Translation("Byte size"),

I believe it's RTBC now.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xurizaemon’s picture

patch copy of MR @ 79677d04511fa137838b99f358684cd8aefcceb2

xurizaemon’s picture

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Rinku Jacob 13’s picture

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

Gauravvvv’s picture

Updated attributions.

Status: Needs review » Needs work

The last submitted patch, 32: 2928801-32.patch, failed testing. View results

xurizaemon’s picture

Issue summary: View changes

Updated description to incorporate rename.

@Rinku, appreciate the nudge. I can't tell what prompted the re-roll - the patch in #29 applied identically for me when I tested it against 10.1.x. Was it not applying for you?

xurizaemon’s picture

Status: Needs work » Needs review

I'm setting this back to Needs Review. It was set to "Needs work" by the patch in #32 triggering tests which failed, but AFAICT the patch doesn't introduce any meaningful change from the previous patch.

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!

If your intent was simply to trigger a test re-run vs 10.x, you'll be glad to know you can do that by clicking "Add test / retest" on the existing patch - please do that in future, as it saves effort from others in comparing changes between old and new patches.

Tests pass once more now that the MR is refreshed. Unsetting patch visibility as the MR is up to date.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

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

hctom’s picture

@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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

xurizaemon’s picture

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.

ericgsmith made their first commit to this issue’s fork.

ericgsmith’s picture

Status: Needs work » Needs review
FileSize
1.25 KB

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 #2157945: Deprecate format_size() and use Drupal\Core\StringTranslation\ByteSizeMarkup instead 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?

benjifisher’s picture

Status: Needs review » Needs work
Issue tags: -Needs usability review

Usability review

We discussed this issue at #3412373: Drupal Usability Meeting 2024-01-12. 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.

ericgsmith’s picture

Status: Needs work » Needs review

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.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Opened a follow up for the discussion of moving the formatter #3427617: Discuss moving formatter from file module

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...

  • longwave committed e97d6c18 on 11.x
    Issue #2928801 by xurizaemon, ericgsmith, geek-merlin, RoSk0, Gauravvvv...
longwave’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

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.

benjifisher’s picture

@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.

Status: Fixed » Closed (fixed)

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