Problem/Motivation
In #3197482: Update Drupal 10 to depend on Symfony 5.4 (as a stepping stone to Symfony 6, for deprecation checking support), the Symfony 5.4 update is failing with this:
Method "Symfony\Contracts\Translation\TranslatorInterface::trans()" might add "string" as a native return type declaration in the future. Do the same in implementation "Drupal\Core\Validation\DrupalTranslator" now to avoid errors or add an explicit @return annotation to suppress this message.
Method "Symfony\Contracts\Translation\TranslatorInterface::getLocale()" might add "string" as a native return type declaration in the future. Do the same in implementation "Drupal\Core\Validation\DrupalTranslator" now to avoid errors or add an explicit @return annotation to suppress this message.
Proposed resolution
Revert #3231683: [Symfony 6] A number of methods of the class Drupal\Core\TypedData\Validation\ExecutionContext are considered internal and Drupal should not override them. to use our own TranslatorInterface instead.
We tried to ague to make Symfony more extensible / consistent in this regard so we don't need to do this, but that was not accepted at https://github.com/symfony/symfony/pull/44911
Remaining tasks
Review. Commit.
User interface changes
API changes
See release notes snippet.
Data model changes
None.
Release notes snippet
Numerous formerly deprecated Drupal methods are not deprecated anymore due to changes in Symfony 5 that made incompatibility unfeasible otherwise:
- Drupal\Core\TypedData\Validation\ConstraintViolationBuilder and its methods were deprecated in 9.3.0 but is no longer deprecated
- Drupal\Core\TypedData\Validation\ExecutionContext and its methods were deprecated in 9.3.0 but is no longer deprecated
- Drupal\Core\TypedData\Validation\ExecutionContextFactory and its methods were deprecated in 9.3.0 but is no longer deprecated
- Drupal\Core\Validation\DrupalTranslator::transChoice() was depreacetd in 9.4.x-dev but is no longer deprecated
- Drupal\Core\Validation\TranslatorInterface was deprecated in 9.3.0 but is no longer deprecated
Comment | File | Size | Author |
---|---|---|---|
#33 | interdiff_14-33.txt | 2.91 KB | Spokje |
#33 | 3255245-33.patch | 39.05 KB | Spokje |
| |||
#27 | 3255245-14.patch | 36.21 KB | Gábor Hojtsy |
#22 | interdiff-3255245-4-22.txt | 49.78 KB | daffie |
#22 | 3255245-22.patch | 51.7 KB | daffie |
|
Comments
Comment #2
longwaveUgh, so what do we do here? The upstream TranslatorInterface is
However we return a TranslatableMarkup or PluralTranslatableMarkup object. We could just cast these to string, but we also protect against translating a previously-translated string - this will be bypassed if we cast the return value.
Comment #3
longwaveWe used to have our own TranslatorInterface that we deprecated in #3231683: [Symfony 6] A number of methods of the class Drupal\Core\TypedData\Validation\ExecutionContext are considered internal and Drupal should not override them., maybe that was a bad idea.
Comment #4
longwaveLet's try naively casting to string, as I'm not sure what else we can do to satisfy the upstream interface.
I guess if
$id
and the return value are both strings, Symfony must ensure this is not called twice already - maybe the safeguard is not needed?Comment #6
Taran2LThis piece of core looks pretty weird tbh, see https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/lib/Drupal/...
DrupalTranslator implements TranslatorInterface, but TranslatorInterface has only methods (
trans
andgetLocale
), but comments state opposite, for example:Seems like this is old code, that is not used anywhere.
Then, more to add, DrupalTranslator itself is not widely used. Actually, I have found only core usage and single contrib module, according to this: http://grep.xnddx.ru/search?text=DrupalTranslator&filename=
Thus, it might be changed more dramatically between 9 and 10.
Comment #7
longwave::transChoice() was deprecated in Symfony 4, I raised #3255250: [Symfony 5] TranslatorInterface::transChoice() is deprecated to deal with that separately.
This code is glue between Drupal's translation system and Symfony's; Symfony's validation engine (used by typed data) requires a class compatible with Symfony TranslationInterface, but otherwise Drupal uses its own translation system.
Comment #8
Taran2Lthis is a very interesting topic, as currently
Symfony\Contracts\Translation\TranslatorInterface
comes not from regular Symfony packages, but on contracts ones, in particular in D9 it comes fromsymfony/translation-contracts
and has version v2.5.0 (with my live 9.3.0 project) and it already do not have::transChoice()
, so it's like that for a long time and it probably can be just safely removed.See https://github.com/symfony/translation-contracts
Comment #9
Taran2LProbably, in D10 those contracts packages should be updated to version
^3
Comment #10
longwaveThe fails here are all where HTML appears in the error message, it is now being escaped when it shouldn't be. For example in CommentAnonymousTest:
Because the placeholder HTML is escaped:
Not sure yet where this is happening, nor how to solve this.
Comment #11
longwaveAdding some related issues where this was first introduced.
Comment #12
longwaveI think the quick fix is to revert #3231683: [Symfony 6] A number of methods of the class Drupal\Core\TypedData\Validation\ExecutionContext are considered internal and Drupal should not override them. and then we don't need to implement Symfony's TranslatorInterface, instead we use our own.
Comment #13
longwaveReverted:
#3248809: [Symfony 6] The Drupal\Tests\file\Kernel\FileItemValidationTest fails with Symfony 5
#3231683: [Symfony 6] A number of methods of the class Drupal\Core\TypedData\Validation\ExecutionContext are considered internal and Drupal should not override them.
Comment #14
longwaveFix deprecation skip.
Comment #15
andypostMaybe it's just unlocked some issue in comment module?
Comment #16
longwave> Maybe it's just unlocked some issue in comment module?
That was just an example, all 40 fails in #4 are HTML that is now incorrectly escaped.
Comment #17
longwaveIf this revert goes in it looks like we can remove our dependency on
symfony/translation
, we don't use it anywhere else, not sure why we depended on it in the first place.Comment #18
murilohp CreditAttribution: murilohp at CI&T commentedHey @longwave, this is just a suggestion, but if we follow the approach of removing
symfony/translation
, what do you think about typehint our own interface to return aMarkupInterface
instead of string(patch #4, methodtrans
).Comment #19
murilohp CreditAttribution: murilohp at CI&T commentedThe following review is from the patch #4, and I'm assuming we're going to keep the idea of type hinting the class.
What do you think about removing the unnecessary
$this->locale
here? I mean use something like:return $this->locale ?? \Drupal::languageManager()->getCurrentLanguage()->getId();
Comment #20
daffie CreditAttribution: daffie commentedReverting #3231683: [Symfony 6] A number of methods of the class Drupal\Core\TypedData\Validation\ExecutionContext are considered internal and Drupal should not override them. and doing mostly the same thing as Symfony, only a little bit different does not make me very happy. To me, we should see if we can do the same thing as Symfony. Drupal is a Symfony app.
Bumping this to critical as this issue is blocking Symfony 6 in D10.
Comment #21
daffie CreditAttribution: daffie commentedWorking on this.
Comment #22
daffie CreditAttribution: daffie commentedThe patch is the same as the patch from comment #4, only with failures fixed.
Edit: The change from variables with
%value
to@value
is necessary because variables that start with an "%" will get '' added before the value and '' added at the end. Variables that start with "@" do not get that. The required return type hint by Symfony make it so that less is possible. The problem is only for validation contstraint violations messages. To me, this is something we shall have to learn to accept. Not that I am happy about it.Comment #23
murilohp CreditAttribution: murilohp at CI&T commentedHey @daffie I liked your solution, it's the original approach and I think we should follow the idea of doing the same thing as Symfony.
About the code:
Thanks! For the change!
+1 for me to RTBC this!
Comment #24
Gábor HojtsyHm, is that the only way to go for validation constraints? That sounds like a stepping stone that could lead to more things that we need to change in the future.
Comment #25
alexpottI've opened https://github.com/symfony/symfony/pull/44911 so we can see what Symfony thinks about extending the scope.
Comment #26
catchGiven the direction that https://github.com/symfony/symfony/pull/44911 is taking (going nowhere), I think we should do #3255245-14: [Symfony 6] Revert 3231603 to use our own TranslatorInterface here.
Then we should open a follow-up to track whether there's an alternative approach, and also revisit #3054535: Discuss whether to decouple from Symfony Validator again.
Comment #27
Gábor HojtsyThat makes sense. Let's send in #14 again for another round then :)
Comment #29
SpokjeLooks like #3255250: [Symfony 5] TranslatorInterface::transChoice() is deprecated doesn't like the way this issue is evolving...
Comment #30
SpokjeOuch, but looking at #3255250-3: [Symfony 5] TranslatorInterface::transChoice() is deprecated this approach might be better anyway.
Comment #32
Gábor HojtsyThe fails are all seem to be due to the transChoice() method missing, that the patch removes :) I don't think we should remove it, just not deprecate it like the rest of the things we are not deprecating?
Comment #33
SpokjeThanks @Gábor Hojtsy, you're right, looks like undeprecating (my English spellchecker is hating me even more today...) is the way to go.
Let's see if I got it right this time
Comment #34
SpokjeComment #35
longwaveThe revert patch looks good, but the issue title is now incorrect and the summary needs an update.
Comment #36
hmendes CreditAttribution: hmendes at CI&T commentedUpdating the title and the proposed resolution from the IS with the comment #12
Comment #37
Gábor HojtsyAdded a release notes snippet as well and further expanded the issue summary.
Comment #38
daffie CreditAttribution: daffie commentedAll code changes look good to me.
The patch is reverting the patch from #3231603: Allow reducing scope for selected modules.
The problem from that issue now need to be fixed in another way. I have created #3258380: [Symfony 6][Second try] A number of methods of the class Drupal\Core\TypedData\Validation\ExecutionContext are considered internal and Drupal should not override them. for that problem.
I have linked this issue to the CR from the issue we are reverting. That CR needs to be unpublished.
For me it is RTBC.
Comment #39
alexpottCommitted d28e89b and pushed to 10.0.x. Thanks!
Committed 1201aa8 and pushed to 9.4.x. Thanks!
Committed 4bb9f46 and pushed to 9.3.x. Thanks!
I fixed the conflict in 9.3.x core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php - hopefully tests won't fail but if they do - I'll hotfix.
Comment #43
Gábor HojtsyThanks for updating the change record, I further updated it to make the undeprecation more visual and updated the title: https://www.drupal.org/node/3238432 Should we have a dedicated change record for the undeprecation?
Comment #44
daffie CreditAttribution: daffie commentedNo, I do not think so. Or did we do an official release with Drupal with original patch in it?
Comment #45
Gábor HojtsyYes our deprecation was released in 9.3.0.
Comment #46
catchSpot checked some methods from the CR:
http://grep.xnddx.ru/search?text=markConstraintAsValidated&filename= 0 references
http://grep.xnddx.ru/search?text=setConstraint%28&filename= - 1 reference in a test
http://grep.xnddx.ru/search?text=isGroupValidated%28&filename= - 0 references
http://grep.xnddx.ru/search?text=isConstraintValidated%28&filename= - references
Let's leave it as is :)