Problem/Motivation
format_size() is still a procedural function. Convert it to a object oriented code.
Proposed resolution
- Add a
\Drupal\Core\StringTranslation\ByteSizeMarkup::create()method that returnsTranslatableMarkup - Deprecate and remove usages of
format_size(). - Add related PHP unit tests.
Remaining tasks
review/commit
User interface changes
none
API changes
format_size()is deprecated- A new
Drupal\Core\StringTranslation\ByteSizeMarkupclass is added
| Comment | File | Size | Author |
|---|
Issue fork drupal-2157945
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:
- 2157945-convert-formatsize-to
changes, plain diff MR !2990
Comments
Comment #2
plachLet's see whether a service works.
Comment #5
plachbah
Comment #7
plachThis should be better.
Comment #9
amateescu commentedUtility classes are not usually exposed as services, is there any reason this one needs to be?
Comment #10
plachWell, it needs the string translation service. I tried also a singleton approach but was not very happy with it...
Comment #11
plachThis time for reals :)
Comment #12
tim.plunkettThis won't work for localize.d.o. It *has* to be a method called t(), used like $this->t().
You can copy/paste one of the various other t() methods with their shortened documentation, like in FormBase, until traits happen.
Missing data types
public function
I'm not sure this is the best place for this. Every other Utility class contains only static methods, without any dependency on other services.
Comment #15
plachThis should fix test failures and address #12, except for:
Drupal\Core\Utility\Service\Size?
Drupal\Core\Service\Size?
Drupal\Core\Service\Utility\Size?
or (less self-documenting):
Drupal\Core\Common\Size?
Comment #16
dawehnerI wonder whether we should have the same dataProvider just reused multiple times?
Comment #17
plachI was wondering that too and I had also an (apparently) good reason for not going that way, but I cannot recall it atm :)
Edit:
Oh, yes, I didn't want to split this out in its own method...
Comment #18
plachI hope the comment typo was intentional, or was it a recursive comment? :)
Comment #19
dawehnerusing \Drupal\Core\Utility\Size is totally fine. We have other utility services like the link generator which aren't static ones.
Worked on making it more phpunit-ish
Comment #22
dawehnerLet's see whether this is enough.
Comment #23
dawehner.
Comment #25
sunI agree that we should do something about these helpers, but a "size" service (name) looks/sounds suboptimal.
In essence, these helper functions fall under the umbrella of language/locale dependent (string) output formatting (templating).
In addition, the output is meant for HTML - and may not be suitable for e.g. CLI output. However, code that generates plain text output for e.g. CLI often needs the identical helper functions, but sans HTML output escaping. Sorta like
HtmlLocale extends Localeor similar.FWIW,
format_date()& Co fall under the same umbrella.Comment #28
jhedstromThis was partly done (https://www.drupal.org/node/2253127).
Comment #30
jhedstromThis adds
Bytes::format()Comment #32
jhedstromInteresting fail here:
which is caused by the class
Drupal\Core\StringTranslation\TranslatableMarkup...Comment #33
mondrake#32:
That's because you cannot call classes in the Drupal\Core namespace from the Drupal\Component namespace. The component would no longer be a 'component' (i.e. independent from Drupal), in that case.
We faced similar discussions in #2583041: GD toolkit & operations should catch \Throwable to fail gracefully in case of errors, comments #27 and #29.
With the patch here I am extracting a part of #2583041: GD toolkit & operations should catch \Throwable to fail gracefully in case of errors that can work in isolation and should be able to address the main point here, but does not get to the point of deprecating
format_sizeand replacing it with a class. But that may be a next step.Interdiff not relevant.
Comment #34
catchCan create the component without the format method. Then add a \Drupal\Core class with a format method, extending from the component. Or move the whole lot into \Drupal\Core is another option.
Comment #36
mondrakeThis patch follows on #33.
We have:
Bytes::toUnitAndSize()method to return separately a unit of measurement and the size expressed in the unit of measurement, given a byte quantity. Added unit test for that.FormatBytesSizeclass in \Drupal\Core\Utility with aformatmethod where we callBytes::toUnitAndSize()and format the size string. Adjusted test for that, and added test cases for formatting '0 bytes' and a quantity of bytes that informat_sizewas resulting improperly rounded.format_size()to useFormatBytesSize::format().format_size().Comment #38
mondrakeYes, yes
Comment #44
mondrakeRerolled.
Comment #48
mondrakePlain reroll of #44.
Comment #50
claudiu.cristeaNits.
Needs a @see reference to the CR. See https://www.drupal.org/core/deprecation
Should yell a @trigger_error(). See https://www.drupal.org/core/deprecation
Do we need to update the component composer.json description?
Quantity? Sounds weird. Amount? Same. Maybe other word? Maybe "count"?
Feels like The NULL explanation should go to the end of docs.
Cannot refer a deprecated function.
s/(Optional)/(optional)
Comment #52
claudiu.cristeaAnd we should replace all occurrences of format_size().
Comment #53
mondrakeAdded draft CR https://www.drupal.org/node/2999981, working on #50
Comment #54
mondrakeThank for review @claudiu.cristea.
#50.1 done
#50.2 done
#50.3 I do not think we need to, 'other manipulations' seems good enough
#50.4,5,6,7 done
added a deprecation test for format_size, too.
Let's see how may deprecation error this gets, probably quite a few. Not sure we should do #52 here. Probably better adding a deprecation listener here and remove usages in a follow up.
Comment #55
mondrakeWith all usages replaced.
Comment #58
mondrakeFix for failure in #55 + CS cleanup.
Comment #59
claudiu.cristeaNice!
According Drupal core deprecation policy, the CR link should be added also as
@see, after@deprecated.The message should end with a dot.
I'm not sure about the name FormatBytesSize. What is the generic name to describe something that is measuring in bytes, KB, MB, etc? Sometime is called "filesize" but that would be restrictive. Digital size? I see here https://en.wikipedia.org/wiki/Units_of_information the term "units of information". Or "storage size". For example if we use digital size, the the function call would be DigitalSize::format(), which for me would be more meaningful than FormatBytesSize::format(). StorageSize::format(). No idea :)
Comment #60
mondrake#59.1 done
#59.2 not sure... see #2848137-66: Replace all calls to db_update, which is deprecated
#59.3 how about
\Drupal\Core\Utility\Bytes::toTranslatableString?Comment #61
claudiu.cristeaI think https://www.drupal.org/core/deprecation is the official reference. And the "official" pattern they are provided there for @trigger_error() ends with a dot.
#59.3: I was thinking on the FormatBytesSize name. When you see in a caller FormatBytesSize::format() it doesn't look great. But it's probably a matter of taste. I'm not insisting to change that.
Comment #62
mondrake#59.2 done
#59.3 et later: I also didn't like the naming. Doing here what @catch suggested in #34. Added a Core/Utility/Byte class extending from Component/Utility/Bytes, with a single
toTranslatableStringmethod. SoFormatBytesSize::format=>Bytes::toTranslatableStringComment #64
mondrakeSorry wrong interdiff in #62, here's the good one.
Comment #67
alexpottI'm not sure about the direction this patch has taken.
Having both
Drupal\Component\Utility\BytesandDrupal\Core\Utility\Bytesseems very weird. And unnecessarily importing the core version is odd too. How about doing something like TranslatableMarkup but as a TranslatableSize? And the new TranslatableSize class could contain all the logic from format_size()?I think the patch here is unnecessarily complex and introduces new value objects like BytesUnitAndSize for little to no gain. We just have more API to support.
Comment #68
alexpottOr do something like date_format()...
And introduce a size.formatter service.
Comment #69
mondrakeThanks for review @alexpott. I will go for the service then.
Comment #70
alexpott+1 and then we get to properly inject the translation service too.
Comment #71
mondrakeConverted to using a
byte_count.formatterservice and removed the BytesUnitAndSize value object.No interdiff, it would make no sense given the extent of changes.
Comment #72
alexpottLooking at this makes me wonder if we should add this to \Drupal\Core\StringTranslation\TranslationInterface as a formatSize() method. Because it is always being used when you are outputting translated strings to the user. Or maybe we could introduce a new placeholder for FormattableMarkup that does translation for TranslatableMarkup.
Also this code
Is wrong. Because you need to enter a valid PHP file size so it shouldn't be translated. So this is an actual place where we should use new thing added to the utility. But that's not returning anything in a useful format.
This is odd. And an API change - before if you passed a negative number this would not happen.
This is mixing a bugfix with a refactor which is never the right thing to do.
Comment #73
mondrakeLet’s fix the bug first then, instead of refactoring it... :)
Comment #78
mondrakeFor now, just a reroll of #71 to see if it still passes. #72 next.
Comment #80
mondrakeDropping the
Bytes::toUnitAndSizemethod in favour ofBytes::toString, stealing from @longwave patch at #3001402-2: editor_image_upload_settings_form() is wrongly translating the max file size.Comment #82
volegerreturn $size . ' B'];Needs more attention
Comment #83
mondrakeIndeed.
Comment #84
andypostLooks great just needs more refactor to inject service and use local variable to cache calls to `\Drupal::service()`
This should use protected var for caching, and probably update constructor to inject new service but keep BC
Please use local var instead of "disturbing" \Drupal so many times
same here
Comment #85
mondrakeRe. #72, removed the ByteCountFormatter class and service in favour of a new
TranslationInterface::formatSizemethod.Leaving service injection suggested in #84 for later.
Comment #86
mondrakeAdded a
formatSizemethod ontoStringTranslationTrait. This way we can get rid of wrapperformatSizemethods where we have translatability! Also added formatSize implementation for a test TranslationManager class.Adding 'needs issue summary update' since IS is now pretty old.
Comment #89
andypostFix migrate tests + minor clean-ups
Comment #92
andypostNW for tests
that needs unit test coverage
new method also needs tests
This duplicate of implementation still looks weird but I have no idea how to get rid of it, maybe mocking
Comment #93
jofitzPatch from #89 no longer applies. Re-rolled.
Comment #95
mondrakeAdding unit test for TranslationManager::formatSize.
Comment #96
mondrake#92.1 - added test coverage for StringTranslationTrait::formatSize
#92.3 - removed the duplicate implementation, just return the $size argument should be enough for the purposes of this test
Comment #101
mondrakeRerolled.
Comment #102
claudiu.cristeaNits & a question.
The "instead" word can go one line up.
Within single quotes there's no need to escape the backslash (
\\Drupal::).I wonder if it's worth considering an external solution such as https://github.com/gabrielelana/byte-units. I have the feeling that we're reinventing the wheel.
Not so happy with extracting the value from a formatted string. Normally that value should be provided directly from a API/method/function.
Comment #103
mondrakeThanks @claudiu.cristea.
1. Done
2. Done
3. Very nice... but that would be a dependency addition, probably to be dealt with separately, and require postponing this one on that. I suppose we need a core committer's blessing for that. Adding tag for framework manager review.
4. Earlier patches were returning a value object here instead, but that were required to be dropped in #67.
Comment #104
alexpottThe dependency addition should definitely separately and probably will prove very tricky because these are translatable strings. For instance google suggests that the Japanese translation from 1mb is 1メガバイト
Comment #105
alexpottDone the framework manager review for the dependency addition. Rule of thumb "don't add things in a refactor".
Comment #106
claudiu.cristeaOK with that. Just a note: I was not referring to the translatable part but to the component. From my point of view this is ready.
Comment #107
alexpottActually I'm not sure about this addition. Providing this means that implementations might use this over the translated version - which would be wrong. Imo this shouldn't be public API.
Comment #108
claudiu.cristeaRe #107. Yeah, agree. Making it a protected method somewhere, would solve also #102.4.
Comment #109
mondrakeWe need that, though? See #72 and the follow-up #3001402: editor_image_upload_settings_form() is wrongly translating the max file size
Comment #111
alexpott@mondrake that's a fine point. We would need very very clear docs on both methods about when and were they should be used. But on the other hand I thins maybe the simplest thing is to not do this. And in https://www.drupal.org/project/drupal/issues/3001402 provide a langcode of 'en' for the translation to not translate it. We should however add docs to TranslationInterface::formatSize() about when it is appropriate to add a langcode = 'en' option - ie. when you when to suggest a value for php.ini etc...
Comment #112
mondrakeOK, so will remove
Bytes::toStringand keep the implementation inTranslationManager::formatSize- even a protected method there could be misused.Comment #113
mondrakeHere we go. In the end I sticked to a protected method on TranslationManager, so that we can test it independently.
Comment #115
mondrakeNeeded a re-roll.
Comment #118
mondrakeRereolled.
Comment #120
mondrakeConverted additional usages introduced since #115 and updated the deprecation message.
Comment #122
mondrakeConverted a stray
getMock.Comment #123
andypostrtbc+1 except
the format a bit different from #3024461: Adopt consistent deprecation format for core and contrib deprecation messages and phpcs
Comment #124
mondrakeAdjusted deprecation message according to #123
Comment #125
andypostCR updates should be done after commit
Comment #127
mondrakeRerolled and changed another usage just introduced.
Comment #129
volegerJust reroll
Comment #130
mondrakeUpdated the IS with the current solution.
Comment #135
shashikant_chauhan commentedRerolled the patch.
Comment #137
mondrakeThanks for the reroll, @shashikant_chauhan
Comment #138
volegerDeprecation message requires updates.
... in drupal:9.1.0 and is removed from drupal:10.0.0.Comment #139
andypostUpdated CR https://www.drupal.org/node/2999981/revisions/view/11116408/11659397
And here re-roll with updated core ref
Comment #141
kostyashupenkoReroll against 9.0.x
Comment #145
martin107 commentedJust a reroll.... lots of automerging .. no conflicts
Comment #146
martin107 commentedA little fixup.
Comment #148
martin107 commentedThe problem :-
In system.install system_requirements() -- the container is not yet available. That old chestnut.
\Drupal::translation() returns null .. and errors
While I think about ways to get correctness .. I am just backing out that change.
Other changes in 146 are correctly.
Comment #149
martin107 commentedHere is the interdiff.
Comment #150
martin107 commentedfewer failing tests.
Comment #153
longwaveMinor reroll following #3131258: Remove redundant $message from assertFile(Not)Exists and assertDirectory(Not)Exists calls
Comment #154
daffie commentedThe function
format_size()is being deprecated.A
@trigger_error()is added to the function.A deprecation message test is added for the
@trigger_error().The functionality is being moved to the method
Drupal/Core/StringTranslation/TranslationManager::formatSize()and the helper methodDrupal/Core/StringTranslation/TranslationManager::bytesToSizeAndUnit().For both methods is testing added.
The main method is added to the interface
Drupal/Core/StringTranslation/TranslationInterface.All function calls to
format_size()have been replaced.All changes look good to me.
Their is an CR added and it looks good to me.
For me it is RTBC.
Comment #156
martin107 commentedReroll, no conflicts just automerging.
Comment #161
andypostRe-roll and added types to arguments of new method
Comment #162
andypostMissed to fix translation manager
Comment #164
andypostThe change to int was wrong, it can accept floats and strings (as number)
moreover https://wiki.php.net/rfc/string_to_number_comparison is mostly accepted
Comment #165
mondrakePlain reroll of #156. This issue has been hopping on and off the RTBC queue for the last year. I wonder what's preventing commit.
Comment #168
alexpottGiven the tiny number of usages of this method in core I'd be tempted to not add this method to the trait in this issue and call $this->getStringTranslation()->formatSize() instead.
We need to add:
'1 GB' is not an int.
We to add typehints - probably just for langcode. $size can be a string, int or float. I think we can add a return typehint of TranslatableMarkup because PluralTranslatableMarkup inherits from that.
This was added in #36 without explanation. Is this expansion of API necessary here? I'm not convinced and can't find any explanation of why on the issue. Why isn't this inlined as before? If we add this anywhere it should be on the Bytes class - as this method has nothing to do with translation (in and of itself). But then people might be tempted to use it when really we want people to use the translated string. I think this should be inlined again unless there is a concrete reason.
If we do add this we need scalar typehints and a return typehint.
Out-of-scope here. This change is not touching Bytes::toInt()
formatSize()is a good name. There are many sizes - tshirt sizes and query counts to give examples that are both sizes but should never be passed to this method. Perhaps we should take this opportunity to improve upon the name. formatByteSize() for example is more descriptive.Comment #169
mondrake#168.5 I vaguely remember discussions at the time on another issue to allow different precisions in the formatting, but yea, oos here. I think I went for a separate method to allow testing it separately - I guess it can be reverted.
#168.7 that was kinda suggested earlier around #58-ish, too.
Comment #170
andypostHere's fixes for type-hints 161-162 with fix #168 (3, 4, 6)
Working on rename
formatByteSize()Comment #171
alexpottWhoops I got this wrong. I was thinking of bytes::toInt() which is kinda the opposite of this. This actually is an int I think. Sorry.
Comment #172
andypostHere's a rename, let's see what bot will say, will explore more strict interface tomorrow
@alexpott As #162 tests show it's not always int, not sure about float but it could be numeric string
Comment #173
andypostYes,
$sizeis passed as float as well in tests at least (otherwise it can't fit into big int)EDIT Ref https://www.php.net/manual/en/language.types.integer.php#language.types....
For file item test the fix is type cast database value to int because db-layer returns ints as strings
Comment #175
andypostFix broken test
Comment #176
alexpott@andypost ah yeah - i see. If we had union types I think we'd have int|float here - because it does need to be a number type. See the docs for abs().
Comment #177
mondrakeThanks @andypost
Do we then agree to keep the method on the trait, re. #168.1? In any case, AFAIK, if we typehint
string $langcode = NULL, we cannot pass a NULL to $langcode - it's only going to be initialized to NULL if the argument is missing. Different would be if we typehinted?string $langcode = NULL, in that case it is possible to pass a null argument. So the doc is incorrect, it should be@param string $langcode.same as above (for the $langcode typehint)
should be removed and its code inlined per #168.5
can be dropped when inlining the method's code.
Comment #178
alexpott@mondrake see https://3v4l.org/VYaKq -
string $langcode = NULLand?string $langcodeare exactly the same (as far as I know).Comment #179
mondrake#178 thanks. I thought I had this discussion already but it turns out it was in Doctrine/DBAL, not Drupal: https://github.com/doctrine/dbal/pull/2766
Does not really matter here, but I tend to agree if we want the argument to be nullable AND default to NULL, then we should add the ? too.
Comment #180
andypostFix for nullable string, it's already used in core https://git.drupalcode.org/project/drupal/-/blob/9.1.x/core/modules/sear...
Re #176 https://3v4l.org/mdHOv - it consumes any number (stringable numbers as well)
EDIT I was wrong about mixed types https://3v4l.org/Dp4VH
Comment #181
andypostInlined method, also found strange that "factional bytes"
Comment #182
mondrakeWe should pass 0 not '0', also later for '0.6'. Let's respect the types.
I think this is just a bug now, we should not have fractions of bytes... should be '0 bytes' in both cases I would say.
can we use
$langcode ?? ''instead ofis_null($langcode) ? '' : $langcode?: voidreturn typehintsame
: arrayreturn typehintpublic function testFormatByteSize(int $size, string $langcode, string $expected): void {Comment #183
andypostRe #182
1) see #180 about strings and numbers - it's just extra test coverage
2) that's BC and not sure how to deal with it
Remains will address, thanks!
Comment #184
andypostFix for #182 (except 2 point) - if we round/ceil/floor it to int then new method breaks BC
Also cleaned a bit doc blocks
Comment #186
andypostAdd more refs to deprecated function and fix title/summary with new name
Comment #188
mondrakeIMHO
1) we should file a follow-up to fix the bug in #182.2 - yeah let's not fix a bug in a refactor, but a file size of "345.56 bytes" does not make sense
2) we might consider a conditional deprecation to trigger an error if $size is a string, to be replaced in D10 by an \InvalidArgumentException. That way when time comes we may typehint $size to
int|floatin PHP8+3)
int|float|string
?string $langcode, string $expected
Comment #190
mondrakeComment #191
andypostFiled follow-up and added TODO #3161118: Make \Drupal\Core\StringTranslation\TranslationManager::formatByteSize() to return integer amount of bytes
Fixed #188 but #188.1 is wrong about string, it expected to be
int|floatnumeric strings are valid float and integeres are part of PHP, see union types at https://3v4l.org/Dp4VHComment #192
mondrakeThanks @andypost - re. #188.3.1 the point is that in the dataprovider one of the input records has ‘0.6’ as $size which is a string. But OK, rather minor one this one.
Comment #193
andypostOne more related to conversion, which exposes one more rounding issue
Comment #195
mondrakeReroll.
Comment #197
mondrakeLet's hope this gets in sometime (#165)... Rerolled.
Comment #203
ankithashettyRe-rolled the patch in #197. Thanks!
Comment #205
ankithashettyRe-rolled the patch in #203...Thank you!
Comment #207
andypostFixed following review, last reroll made first 2 points wrong
This change is wrong and broke test
should be moved out of loop
that's tricky about BC but should be fine
Comment #210
andypostRe-roll for https://www.drupal.org/node/3176667
Comment #213
catchDiscussed this with @alexpott and we think there's a way to avoid introducing the additional API surface here.
format_size() is only used in string translations. So instead of manually calling a method on the arguments to t(), could we look at adding a new placeholder in translated strings - we'd then pass the raw bytes as argument and let the translation system do the conversion via the placeholder. Would mean all the logic staying in a protected method instead of a public one.
If we were to do that later, we'd need to then deprecate the new methods added here, so it seems worth trying to address here if we can.
The actual change for developers then looks something like this. One disadvantage is this changes the translated strings, but since we'd only change this in a minor release anyway that's not a problem.
Note that
^placeholderisn't really a suggestion, it's just to have something to look at.Comment #214
andypostHere's re-roll for 9.2 and re #213
It's mostly for core but it's also used in tokens and render
There's more usage except translation strings
Comment #215
daffie commentedPutting the issue back to RTBC, because the proposed fix by @catch and @alexpott does not work. As explained by @andypost in the previous post.
Comment #216
alexpottI'm not sure I agree with the #214. In fact all those places are still string translations because format_size() always returns an instance of \Drupal\Core\StringTranslation\TranslatableMarkup. For example:
$replacements[$original] = format_size($file->getSize());could be$replacements[$original] = $this-t('#size', ['#size' => $file->getSize()]);We're need to support
%#for placeholdered sizes but that also feels possible and a good thing to solve incase we add support for other placeholders.Comment #217
andypost@alexpott I got your idea, other option could be
ByteSizeTranslatableMarkupso auto-escaping will allow%to apply to valueBringing more "magic" into
\Drupal\Component\Render\FormattableMarkup::placeholderFormat()sounds making it more fragile (todo and deprecation already exist in the method)Comment #218
andypostHere's new approach a-la #217 -
\Drupal\Core\StringTranslation\ByteSizeMarkupwraps logicComment #219
andypostFix CS
Comment #221
andypostFix tests except
StringTranslationTraitTestComment #224
berdirwill need to be kept as a deprecated method.
This doesn't really feel common enough to be worth adding to this trait.
If I counted correctly there are 13 calls to this trait method then in core from 7 classes.
I do agree with @andypost that exposing this (only) as a string translation placeholder feels strange. Additionally might be neat, but I'm sure about adding more magic there, seems like a somewhat unusual pattern to start.
> For example: $replacements[$original] = format_size($file->getSize()); could be $replacements[$original] = $this-t('#size', ['#size' => $file->getSize()]);
Yeah, but we don't do translations that consist of nothing but a placeholder, that's pointless, nothing can be translated here. Seems like a very weird pattern to enforce just so we could only have the placeholder thing.
The class is an interesting idea, but it also feels a bit weird to basically recursively build a TranslatableMarkup object within render() in a subclass of TranslatableMarkup :-/
We don't need the lazy-translation behavior TranslationMarkup here, so seems like we could just as well make it static method or method on a separate service then.
Not sure.
same.
reminder to remove this once the method is gone from TranslationManager.
if we do keep the current trait behavior, that could/should go in \Drupal\Tests\UnitTestCase::getStringTranslationStub() then all of this can be removed.
If we move it to a separate service then we'll have to mock that directly.
Comment #225
andypostneeds update to 9.3
it needs to make sure that size is int or float #3240220-3: \Drupal\file\Entity\File::getSize() can cause deprecations on PHP 8.1
Comment #229
karishmaamin commentedRe-rolled patch against 9.4.x. Please review
Comment #230
karishmaamin commentedFixed custom code failure
Comment #232
alexpottAfter reading @Berdir's feedback in #224 I've moved this patch to generator static method on class pattern. This keeps the code very similar - format_size() is replaced by ByteSizeMarkup::create() - the method generates the same TranslatableString object as format_size() would. There's no other new API. It's not part of the string translation service or the trait. I think this is about the simplest solution we've got. One thing I ponder is whether Drupal\Core\StringTranslation is the correct place for this. Maybe Drupal\Core\Utility is a better location because the translation aspect is really a secondary effect - the major part of this class and reason for using it is to convert a large number of bytes into something more human friendly.
Also we need 9.4.x and 10.0.x patches - once we've agreed on the approach we should refactor the internals in the 10.0.x patch to leverage the match() function is this will make it much easier to read.
Comment #233
alexpottFixing the test groups.
Comment #236
alexpottFixing the test fails.
Comment #237
alexpottBefore updating the issue summary and change record with the latest changes I think we should sort this out. The more I think about the more I'm convinced that this belongs outside the translation system. The fact this thing produces translatable strings is an implementation detail. The important this is about converting a large number of bytes into a readable string. Utility is not a bad place for this - we've got a hodge-podge of things in there like TableSort - which is not that dissimilar - it's a helper for rendering sortable tables with only static methods.
Comment #243
bhanu951 commentedRerolled Patch 2157945-236.patch for 10.1.x branch.
Seems it needs Draft CR Update as well.
If it is not going to make in 10.0 we need to update depreciated message as well.
Comment #244
bhanu951 commentedComment #245
spokjeReroll failed.
Besides that, let's first get a consensus on #237 before we start throwing code at this.
Comment #246
andypostLooks code changes are ready, what's left to polish except change record?
...hiding files as MR has latest changes to review
Comment #247
bhanu951 commented@andypost
There is one review comment, can you pls provide your's feedback on it ?
Comment #248
bhanu951 commentedOne more discussion we need to make is where the class should be placed as commented in #237
From @alexpott :
Comment #249
mstrelan commentedAdded some comments to the MR. More importantly we need to update the title as it no longer reflects the implementation.
Comment #251
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #253
kim.pepperNeeds an IS update as we are now calling
\Drupal\Core\StringTranslation\ByteSizeMarkup::create()instead ofTranslationInterface::formatByteSize()Comment #256
kim.pepperComment #258
kim.pepperAddressed most of the feedback. Still have to look at while
\Drupal\Tests\file\Functional\FileFieldWidgetTest::testMaximumUploadFileSizeValidation()is failing because the value isn't being trimmed.Comment #260
smustgrave commentedRemoving credit from myself as all I did was a rebase to run the tests.
Comment #261
smustgrave commented@kim.pepper I ran the test locally without the space around 5.1 megabytes and they pass. Are they just failing here?
And is that a stopper for this?
Comment #262
kim.pepperLooks like those tests are passing now? Not sure why they were failing before.
Comment #263
smustgrave commentedGoing to go ahead and mark but if that one change could be reverted back (if it matters at all)
Comment #268
andypostI think it ready as all feedback addressed
Meantime the main worries are about strong type requirement for
function create(float|int $size,...)butAs All tests passing we can get more feedback (and address it) withing 10.2 cycle so contrib and custom can fix it properly
Comment #269
andypostChecked uploadprogress code and it returns values as strings (probably will need type-cast) but can't test if it fails or throws deprecation, see https://github.com/php/pecl-php-uploadprogress/blob/master/uploadprogres...
In related there's APCu replacement #1561866: Add support for built-in PHP session upload progress
Moreover uploadprogress working only with Apache2 server and we have no testing env to be sure
Comment #270
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #272
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #273
xjmThis poor issue. Approaching its 10th birthday, and bumping in and out of RTBC for 5 years.
I pruned 135 empty-ish comments (status and metadata changes with no substantive info, test fail comments, etc.) and it still barely renders due to GitLab JS crossposts.
It's also been tagged for framework manager review for 3 years. A couple framework managers have been reviewing it, but not given signoff that I can see. I will reach out to other committers.
Part of the issue is also that it is a total 355 LOC change set, which is pushing the upper bound of reviewability. The conversions could perhaps be moved to a followup to manage the scope? Not going to require that ATM, but let's keep it in mind if this doesn't get signoff and land soon.
Comment #275
catchI hadn't reviewed this since before it moved to a value object/ByteSizeMarkup c. 2021/2022, it looks so much better using that.
I did think ByteSizeMarkup sounds like a cheeky name for some kind of HTML preprocessor - make your markup bite size! Not an actual problem.
There was one remaining question from @alexpott here about whether it should be in the translation or utility namespace. On the one hand yes it's not actually making things translatable (except in the sense it produces a translatable string), but it does seem extremely closely related to TranslatableMarkup and PluralTranslatableMarkup etc. so this feels fine to me. There haven't been any strong opinions since the question was asked, so I think we should go ahead here, seems 50/50 to me at worst. MarkupInterface and friends are in Drupal\Component\Render so that's definitely not the right place, but had to check.
Committed/pushed to 11.x, thanks!
Comment #276
xjm🎉