Problem/Motivation
Instead of using t(), rest classes should use the Drupal\Core\StringTranslation\StringTranslationTrait and call $this->t().
Proposed resolution
Use the Drupal\Core\StringTranslation\StringTranslationTrait whenever possible.
- core/modules/rest/src/Plugin/rest/resource/EntityResource.php
- core/modules/rest/src/Plugin/ResourceBase.php
- core/modules/rest/src/Plugin/views/row/DataFieldRow.php
Remaining tasks
User interface changes
API changes
Original report by @Cyberwolf
Currently Drupal\rest\Plugin\rest\resource\EntityResource calls the function t() directly at several places. This causes troubles in unit tests where Drupal isn't bootstrapped. Instead of t(), EntityResource should use the Drupal\Core\StringTranslation\StringTranslationTrait and call $this->t(). This makes it possible to unit test the EntityResource class properly.
Comment | File | Size | Author |
---|---|---|---|
#36 | interdiff.txt | 754 bytes | balagan |
#36 | entityresource_should-2349805-36.patch | 6.06 KB | balagan |
#31 | interdiff.txt | 1.6 KB | balagan |
#31 | entityresource_should-2349805-31.patch | 6.06 KB | balagan |
#28 | interdiff.txt | 909 bytes | balagan |
Comments
Comment #1
Cyberwolf CreditAttribution: Cyberwolf commentedThe base class uses the trait already, so it's sufficient to replace all calls to t() with $this->t().
Comment #2
clemens.tolboomIt's best to tackle the whole rest module at once.
You missed 2 in other rest files using the trait.
- core/modules/rest/src/Plugin/views/row/DataFieldRow.php
- core/modules/rest/src/Plugin/ResourceBase.php
Should we tackle the other occurrences for t()? t() is not deprecated so I guess not.
fwiw I did a 'Find in path' for the rest directory using
[\( ]{1}t\(
Comment #3
dawehner@clemens.tolboom
So you want to solve each instance inside REST right, afaik this issue was opened to just solve the instances in EntityResource itself.
Comment #4
clemens.tolboom@dawehner your are right about the scope @Cyberwolf fixed but it's base class was not fixed. As there were no followers I thought is was best to do those as well.
@Cyberwolf did a great job at the REST sprint yesterday and be in today probably for 50%.
Comment #5
clemens.tolboomChanging title accordingly.
Comment #6
clemens.tolboomAdded summary and unassigned @Cyberwolf
Comment #7
jhedstromThis looks good.
Comment #8
alexpottActually t() should NOT be used whilst throwing exceptions. Since if t() is broken as well then you can end up hiding the original exception.
[Edit: for correctness - see #14]
Comment #9
balagan CreditAttribution: balagan commentedComment #10
balagan CreditAttribution: balagan commentedI have tried to implement changes suggested in #8
Comment #11
balagan CreditAttribution: balagan commentedComment #12
alexpott@balagan this issue should also remove the usage of t() in those exception messages that #8 shows.
Comment #13
balagan CreditAttribution: balagan commented@alexpott: I am confused. In #8 you wrote "Actually t() should be used whilst throwing exceptions."
If t() thould be removed in the exception messages, then what is wrong with the patch in #5?
Comment #14
alexpott@balagan oops I should have wrote "Actually t() should NOT be used whilst throwing exceptions." - sorry.
Comment #15
balagan CreditAttribution: balagan commentedI still don't understand what is wrong with patch in #5 that uses $this->t(). Should $this->t() be removed as well?
Comment #16
alexpottYep - we should not be translating exception messages ever. This can lead to further exceptions being thrown and can make it very difficult to track down what is really causing the problem.
t() and $this->t() both end up using the translation service - one is dependency injected the other is not. In class we should be using dependency injection to make our code unit testable and its dependencies transparent.
Comment #17
balagan CreditAttribution: balagan commentedThanks for the clear answer, working on a new patch.
Comment #18
balagan CreditAttribution: balagan commentedComment #19
balagan CreditAttribution: balagan commentedI worked from patch in comment #5, so the interdiff is between patch #5 and patch #19
Comment #20
balagan CreditAttribution: balagan commentedComment #22
balagan CreditAttribution: balagan commentedCorrected the wrong quotes.
Comment #23
balagan CreditAttribution: balagan commentedComment #25
balagan CreditAttribution: balagan commentedChanged check_plain to String::checkPlain()
Comment #26
balagan CreditAttribution: balagan commentedComment #28
balagan CreditAttribution: balagan commentedInserted missing dollar sign in front of field_name variable
Comment #29
balagan CreditAttribution: balagan commentedComment #30
alexpottThis should use
String::format('Access denied on creating field @field.', array('@field' => $field_name)
Same as above.
Comment #31
balagan CreditAttribution: balagan commentedThanks for the tip, I fixed all 3 occurences according to #30.
Comment #32
balagan CreditAttribution: balagan commentedComment #33
balagan CreditAttribution: balagan commentedComment #34
areke CreditAttribution: areke commentedLooks good. The patch applies and fixes every issue noted above; it should be ready to go.
Comment #35
tstoecklerMinor, but the space before
String
should be removed.Comment #36
balagan CreditAttribution: balagan commentedRemoving that naughty space.
Comment #37
balagan CreditAttribution: balagan commentedComment #38
tstoecklerAwesome, thanks!
Comment #39
alexpottRemoving usage of t() in exception messages is fixing a bug. This issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed cf00c8c and pushed to 8.0.x. Thanks!