Problem
format_plural is deprecated and should be called through the translation service.
Proposed resolution
Replace direct format_plural calls with function from the translation object: IE:
\Drupal::translation()->formatPlural()
Remaining tasks
Remove deprecated format_plurals function.
API changes
No direct calls to format_plural()
| Comment | File | Size | Author |
|---|---|---|---|
| #91 | remove_deprecated-2309737-91.patch | 66.69 KB | ianthomas_uk |
Comments
Comment #1
millerbennett commentedComment #2
millerbennett commentedAltered patch name to match issue number
Comment #3
millerbennett commentedComment #4
penyaskitotestbot?
Comment #5
penyaskitotestbot?
Comment #6
penyaskitoSadly it does not apply anymore.
Comment #7
penyaskitoComment #8
danylevskyiComment #9
sutharsan commentedRerolled.
@danylevskyi, do you still want to work in this issue? You have it assigned to yourself. There are still some (new)
format_plural()calls in core. You can remove those.Comment #11
Anonymous (not verified) commentedGave this a run through since I happened to be inside the repository.
Comment #12
sutharsan commented@toddmbloom, Adding an "interdiff" (https://www.drupal.org/documentation/git/interdiff) helps others to evaluate your work.
Comment #13
penyaskitoUnassigned for not stopping anyone to work on this one. Please comment if you want to work on this again.
The patch does not apply anymore.
Comment #14
rpayanmWorking on this...
Comment #15
rpayanmComment #16
rpayanmReplacing the function in the comments.
I got a dude, in core/lib/Drupal/Core/Template/TwigNodeTrans.php line 53:
This should be modified?
Greetings
Comment #17
penyaskitoYes, that code is what converts the
{% trans %}{%plural %}twig tag in real php code, so must be a valid function.The css changes should be from a different patch, not relevant here.
Comment #18
rpayanmhehe I mixed two patches, sorry :)
Comment #19
rpayanmComment #20
quietone commentedWorks for me. #18 passes tests and no sign of calls to format_plural function.
Comment #21
dawehnerThank you for working on it!
This is an exeption, let's not use format_plural for that but just an english message which is plural by default.
You can use
$this->formatPluralinstead()Did you considered to use the StringTranslationTrait instead? It provides a
formatPluralmethod already.You can already use
$this->formatPlural()hereThis could also use
$this->formatPlural()Comment #22
rpayanmwow @dawehner thank you :)
and... fixed!
Comment #23
markot91 commentedComment #24
markot91 commentedHey all,
The patch #22 applies without errors. The only place where function format_plural was found in InfoParser.php.
Comment #25
rpayanmYes @dawehner said me:
Then RTBC...
Comment #27
herom commentedActually, you should still remove the format_plural. Like this:
That's probably what @dawehner meant.
Comment #28
rpayanmComment #29
herom commentedI went ahead and made the change. This fixes @dawehner's point 1 in #21.
Comment #31
herom commentedFixed. Removed that test that checked for the singular format, because it was removed.
Comment #32
jaime@gingerrobot.com commentedPatch applied cleanly for me. I grepped and didn't find more references to the function.
Comment #33
dahousecat commentedPatch applied cleanly for me too. There were 3 instances of format_plural in the system module before applying the patch and these were all replaced after the patch was applied.
Comment #34
prashant.cPatch submitted in #31 applied cleanly and all the instances of format_plural is being replaced by \Drupal::translation()->formatPlural().
Comment #35
prashant.cComment #37
quietone commentedReroll
Comment #38
rpayanmFix comments length :)
Comment #41
rpayanmrerolled...
Comment #42
victoru commentedReviewing now
Comment #43
victoru commentedWhy is this needed?
Patch 41 applies correctly, all formal_plural instances were removed, (also checked comments), however there is still an outstanding question related to this - #31
Comment #44
ianthomas_ukHas this been proven to work, as opposed to just not break a test?
This is OO code, so should be using an injected translation object instead of \Drupal::translation()
Comments should wrap at 80 chars and use the interface name, not \Drupal::translation()
Can we use the OO-style in tests? If so, there are lots of tests to update.
OO code, should be using injection instead of Drupal::
This should use the interface name
These changes are all because of the review comment on #21 that we shouldn't bother using format_plural in exception messages. I've not verified that this changes are correct.
Comment #45
hussainwebAttempting a reroll first. I will set to 'Needs work' for comment feedback in #44 once the tests pass. This is a reroll from #38 directly, as #41 was just a reroll.
Comment #46
hussainwebOops... Forgot the file.
Comment #47
ianthomas_ukComment #48
max-kuzomko commentedComment #49
max-kuzomko commentedComment #51
ianthomas_ukMake sure you use the latest checkout of 8.0.x from git (the most recent beta will usually be too old). Your patch is much smaller than the previous one which is a warning sign. Did you get conflicts locally, and lose those changes?
Please attach an interdiff so others can see your changes, see https://www.drupal.org/documentation/git/interdiff
Comment #52
max-kuzomko commented@ianthomas_uk, thank you for assistance.
Now I've cloned the latest 8.0.x branch.
In this case my patch is not a modification of an existing patch. So I don't need provide an interdiff?
Or I'm wrong?
Comment #53
ianthomas_ukYour patch should be a modification of #46, which itself was a reroll of #41 so should address the review comments since then.
#46 is 68.55KB, but #52 is 12.5KB. Why is there a huge difference in size?
Comment #59
ianthomas_ukWhy are you testing old patches? That's just adding noise to this issue and hogging the test bots so other people can't test their patches. It's rare for test results to change, unless the previous test had failed due to a temperamental test (which can happen occasionally) or a patch needs a reroll to apply to HEAD (which means your new test result will just say patch does not apply).
Comment #69
max-kuzomko commentedSorry for the noise.
I tried to use the last passed tests patch - it doesn't work for me.
So I tried to find out which one of the last patches I may use for modification.
Comment #70
ianthomas_ukPlease stop retesting patches like that, it won't tell you anything. #46 is the patch you should use as your starting point.
Comment #71
max-kuzomko commentedOk!
Please, correct me if I'm wrong.
To resolve the issue I need apply #46 patch and resolve the remaining usage of format_plural function?
Comment #72
ianthomas_ukRead and understand the comments above.
- Yes, apply #46 as your starting point, resolving any conflicts that occur.
- If there are any remaining calls to format_plural then they will need to be resolved, but #33 and #34 suggest that there aren't any remaining. Are you sure you haven't just failed to resolve a conflict properly?
- Look for any review comments that haven't been addressed, in this case #43 and #44 (because as #46 says, it was only a reroll and doesn't address those comments).
- Upload your patch and interdiff with a brief note explaining what you've done
Comment #73
max-kuzomko commentedThanks for explaining.
But what I should do if "git apply 2309737-45.patch" returns:
error: patch failed: core/modules/system/system.admin.inc:304
error: core/modules/system/system.admin.inc: patch does not apply
I tried to remove all format_plural usage in #69 from scratch.
Comment #74
cilefen commentedSee https://www.drupal.org/patch/reroll
Comment #75
cilefen commented@max-kuzomko Thank you for working on this issue. If you are a new contributor, consider attending the Drupal core mentoring office hours https://www.drupal.org/core-office-hours. The mentors can explain the process, from start to finish.
Comment #76
max-kuzomko commentedThanks to all for assistance.
Rerolled #46
Comment #77
cilefen commentedComment #78
rpayanmComment #79
hussainwebI am a bit confused with whatever has happened between comments 46 to 78. So, let me start with a reroll of #46.
Comment #80
ianthomas_uk#76 and #79 are identical. I guess that validates the reroll at least ;)
Still needs #43/#44 to be addressed though.
Comment #81
hussainwebFor #43.1: I don't see why that block was removed in this issue. It seems it was removed because there is a similar test just before it, but I think it doesn't hurt to test it under slightly different conditions. Anyway, it may not be in scope of this issue. I reverted that change.
#44.1: I think it should work. Wherever format_plural could be called, the
\Drupalobject would still be in scope. Can you be a bit more specific on how you mean "proven"?#44.2: I am trying to figure this out. There are two methods I can think of:
1. Make
DrupalTranslatora service, so that we can get the service container injected early.2. Directly pass translation object from the caller. There is only one caller -
TypedDataManager, which too does not have a container.#44.3: Changed.
#44.4: Do you mean injecting the translator? I believe we don't need to do that for tests. If the tests mean to use a different container, they typically call
\Drupal::setContainer.#44.5: Same reasons as point 2. Another note here is that this class calls
\Drupalelsewhere directly. I know that this is usually not recommended but I can't think of how to make this a service or inject the container. Any suggestions?#44.6: Done. It looks a bit weird, but I guess we have no choice.
#44.7: I think this is fine. Using format_plural could be subjective but I think we're fine with String::format in this case, especially because we are using String::format for the same reason a few lines before this.
Interdiff and patch attached.
Comment #83
ianthomas_uk#44.1 I mean has anyone run the code (that specific line) locally, and confirmed that it works and does what it's meant to. It's too easy with these sort of patches to just assume the tests will catch any mistakes.
#44.2 If we can't work out how to inject it that doesn't need to block the patch from being committed, we're still making progress.
#44.4 You're right, we can use \Drupal for tests. I hadn't noticed it was test code.
(I've not actually looked at the patch yet, just thought you might appreciate answers to your questions).
Comment #84
prashant.c#81 patch applying failed.
Comment #85
sumitmadan commentedRerolling with the test fix. Hopefully should pass now.
Comment #86
sumitmadan commentedComment #90
hussainwebThis is the failure we are expecting here. We can't add it as we are actually expecting the exception.
The reason for failure in #81 (which was valid) is that we are no longer using format_plural for the exception message. It is expecting
value 'Missing required keys (type) in core/modules/system/tests/fixtures/missing_key.info.txt.' is equal to value 'Missing required key (type) in core/modules/system/tests/fixtures/missing_key.info.txt.'(note the plural keys). I am changing the expected value to match the new actual value.The interdiff is against #85 as it is also a reroll. I have reverted the change to missing_key.info.txt.
Comment #91
ianthomas_uk#90 needed a simple reroll due to a conflict in a test, but is otherwise fine.
I've checked that the line I was worried about in #44.1 does work, although the only current use of it in core is in a test (TwigTransTest, see #2047135: Added support for the Twig {% trans %} tag extension).
We could perhaps use dependency injection in a couple more places (#44.2 and #44.5), but I can't see how we're meant to do that. This at least makes the existing dependency more obvious and easier to refactor due to the use of \Drupal.
All my concerns have been addressed, there are no other calls to format_plural, therefore RTBC.
Comment #92
alexpottCommitted aff9577 and pushed to 8.0.x. Thanks!
The meta issue has the beta evaluation.
Comment #94
alexpottAdd this to the CR https://www.drupal.org/node/2173787 - can people try to add the "remove deprecated x usage" issues to the correct CRs before commits. Thanks.