Problem/Motivation
As found in #2976394: Allow Symfony 4.4 to be installed in Drupal 8, The "Drupal\Core\Validation\TranslatorInterface" interface extends "Symfony\Component\Translation\TranslatorInterface" that is deprecated since Symfony 4.2. With Symfony 5, we should use Symfony\Contracts\Translation\TranslatorInterface instead.
Similar to #3029197: [symfony 5] Class implements "Symfony\Component\DependencyInjection\ResettableContainerInterface" that is deprecated since Symfony 4.2, use "Symfony\Contracts\Service\ResetInterface" instead. Contracts are in Symfony 4 but not in Symfony 3.4.
Proposed resolution
Some options explored in #3029197: [symfony 5] Class implements "Symfony\Component\DependencyInjection\ResettableContainerInterface" that is deprecated since Symfony 4.2, use "Symfony\Contracts\Service\ResetInterface" instead were:
- Avoid using the interface altogether
- Depend on the contracts (they have higher PHP requirements though so not yet possible)
- Introduce an intermediary interface for us for now that contrib can also use
@catch has chosen to avoid using the interface altogether in #13.
Remaining tasks
Discuss.
User interface changes
N/A.
API changes
\Drupal\Core\Validation\TranslatorInterface no longer extends \Symfony\Component\Translation\TranslatorInterface
Data model changes
None.
Release notes snippet
In the interest of supporting deprecations in Symfony 4 leading up to Symfony 5, \Drupal\Core\Validation\TranslatorInterface no longer extends \Symfony\Component\Translation\TranslatorInterface.
| Comment | File | Size | Author |
|---|---|---|---|
| #18 | 3030485-18.patch | 1.67 KB | r.shaikh |
| #14 | 3030485-14.patch | 1.69 KB | r.shaikh |
| #11 | 3030485-7-combined.patch | 163.25 KB | catch |
| #9 | 3030485-7-combined.patch | 163.23 KB | catch |
| #7 | 3030485-6.patch | 1.56 KB | catch |
Comments
Comment #2
catchOK so the issue here is multifold:
1. We can't add Symfony\Contracts as a dependency until we require PHP >= 7.1.3
2. If we just ignore the deprecation, while this gives us a clean test run on Symfony 4 (but only after adapting our deprecation listener for unit tests), we'll get a hard error when we're preparing core itself for Symfony 5.
Another option is to fork the interfaces - both the Symfony 3 one to remove the @deprecated annotation, and potentially the Symfony\Contracts one to be forwards compatible, then make sure they load before Symfony's own versions - although we only need to do the latter for code where our translator is injected into Symfony code that fires additional deprecations, which I'm not sure whether that happens or not yet.
Following patch does just the first part to see if it's sufficient. I might try part 2 anyway just to test the viability of implementing both interfaces.
Comment #4
catchNeed to copy the Symfony 3 version instead of the Symfony 4 one..
Comment #6
catchBad patch file, re-uploading.
Comment #7
catchAnother approach. See https://github.com/symfony/symfony/issues/15714 for why this interface was moved in the first place. Also shows that we originally forked the interface ourselves and tricked composer, then unforked it.
Comment #8
catchIf we got with #7 we'll need to Drupalise the docuementation for that method, tagging 'novice' in case someone wants a straightforward patch to work on.
Comment #9
catchPatch with the S4 update to confirm this works for that version too.
Comment #11
catchGrrr.
Comment #13
catchOK so #7 works, just needs the code style adapting to core standards I think.
Comment #14
r.shaikh commentedUpdated coding standard. have a look and please review it.
Comment #15
r.shaikh commentedComment #16
gábor hojtsy@R.shaikh thanks, getting there! :)
There should not be newlines between @params.
These should have a period at the end of sentence.
Comment #17
r.shaikh commentedComment #18
r.shaikh commented@Gábor Hojtsy thanks for your review. As per your suggestion i have updated patch. kindly review and commit it.
Comment #19
r.shaikh commentedComment #20
gábor hojtsyCode style now adapted, looks good.
Comment #21
gábor hojtsyComment #23
gábor hojtsyIt was a DrupalCI fail, the patch is passing all tests.
Comment #24
alexpottI'm not completely convinced this is the right solution.
With this fix we are maintaining an unnecessary level of indirection. Since we now have most of the validator code in core and override most of the constructors I think we can remove \Drupal\Core\Validation\DrupalTranslator entirely and use TranslatableMarkup in \Drupal\Core\TypedData\Validation\ExecutionContext::addViolation(). However this would mean extensive changes to \Drupal\Core\TypedData\Validation\ConstraintViolationBuilder so perhaps this is worth discussing in a more general followup about whether we should try to do some major upstream work to make it simpler to leverage this component or whether leveraging the component is worth it all. However with the move to contracts maybe more of Symfony will want a translator and this bridge might be more useful rather than less.
So tldr; let's go ahead here but before I commit shall we add an @todo to extend
Symfony\Contracts\Translation\TranslatorInterfacein Drupal 9? And I think we should open a follow-up to discuss the above points.I've checked contrib and nowhere appears to be using DrupalTranslator as a bridge to other Symfony components where a translator is required so that is at least a good thing - sewe http://grep.xnddx.ru/search?text=DrupalTranslator&filename=
Comment #25
catchOK #24 to me seems to be suggesting two things, both of which are viable but which seem opposed to each other:
1. Stop using DrupalTranslator, the Symfony Translation component, or Symfony Contracts altogether - because it turns out we don't need it.
2. Switch to using the interface from SymfonyContracts
I'm pretty sure #1 is viable, so I think we should do that, but I'd lean towards a new issue and no @todo for this, because we need to prove we can do it, otherwise we'd need to do #2, so git blame linking to this issue, and from this to #3035839: Is DrupalTranslator needed? seems enough.
Comment #26
alexpottI'm good with #25. Sorry that #24 was not clearer that I was evaluating different thoughts. Having the one follow-up #3035839: Is DrupalTranslator needed? is good.
Comment #28
gábor hojtsyYay, thanks, committed!
Comment #29
gábor hojtsyLet's add it to the release notes. Added release notes snippet to issue summary.
Comment #31
kattekrab commentedThere's no Change Record here.
The release notes snippet says....
In the interest of supporting deprecations in Symfony 4 leading up to Symfony 5,
\Drupal\Core\Validation\TranslatorInterfaceno longer extends\Symfony\Component\Translation\TranslatorInterface.Comment #32
gábor hojtsyCreated and published change record at https://www.drupal.org/node/3039683