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.

Comments

Gábor Hojtsy created an issue. See original summary.

catch’s picture

Status: Active » Needs review
StatusFileSize
new164.3 KB
new3.01 KB

OK 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.

Status: Needs review » Needs work

The last submitted patch, 2: 3030485.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

catch’s picture

Status: Needs work » Needs review
StatusFileSize
new164.22 KB
new2.87 KB

Need to copy the Symfony 3 version instead of the Symfony 4 one..

Status: Needs review » Needs work

The last submitted patch, 4: 3030485.patch, failed testing. View results

catch’s picture

Status: Needs work » Needs review
StatusFileSize
new2.93 KB

Bad patch file, re-uploading.

catch’s picture

StatusFileSize
new1.56 KB

Another 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.

catch’s picture

Issue tags: +Novice

If 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.

catch’s picture

StatusFileSize
new163.23 KB

Patch with the S4 update to confirm this works for that version too.

Status: Needs review » Needs work

The last submitted patch, 9: 3030485-7-combined.patch, failed testing. View results

catch’s picture

Status: Needs work » Needs review
StatusFileSize
new163.25 KB

Grrr.

Status: Needs review » Needs work

The last submitted patch, 11: 3030485-7-combined.patch, failed testing. View results

catch’s picture

OK so #7 works, just needs the code style adapting to core standards I think.

r.shaikh’s picture

StatusFileSize
new1.69 KB

Updated coding standard. have a look and please review it.

r.shaikh’s picture

Status: Needs work » Needs review
gábor hojtsy’s picture

Status: Needs review » Needs work

@R.shaikh thanks, getting there! :)

  1. +++ b/core/lib/Drupal/Core/Validation/TranslatorInterface.php
    @@ -2,18 +2,39 @@
    +   * @param string $id
    +   *   The message id (may also be an object that can be cast to string)
    +   *
    +   * @param array $parameters
    +   *   An array of parameters for the message.
    +   *
    +   * @param string|null $domain
    +   *   The domain for the message or null to use the default.
    +   *
    +   * @param string|null $locale
    +   *   The locale or null to use the default.
    +   *
    +   * @return string
    +   *   The translated string
    

    There should not be newlines between @params.

  2. +++ b/core/lib/Drupal/Core/Validation/TranslatorInterface.php
    @@ -2,18 +2,39 @@
    +   *   The message id (may also be an object that can be cast to string)
    ...
    +   *   The translated string
    

    These should have a period at the end of sentence.

r.shaikh’s picture

Assigned: Unassigned » r.shaikh
r.shaikh’s picture

StatusFileSize
new1.67 KB

@Gábor Hojtsy thanks for your review. As per your suggestion i have updated patch. kindly review and commit it.

r.shaikh’s picture

Status: Needs work » Needs review
gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Novice

Code style now adapted, looks good.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 18: 3030485-18.patch, failed testing. View results

gábor hojtsy’s picture

Status: Needs work » Reviewed & tested by the community

It was a DrupalCI fail, the patch is passing all tests.

alexpott’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs review

I'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\TranslatorInterface in 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=

catch’s picture

OK #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.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community
Related issues: +#3035839: Is DrupalTranslator needed?

I'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.

  • Gábor Hojtsy committed dd05469 on 8.7.x
    Issue #3030485 by catch, R.shaikh, Gábor Hojtsy, alexpott: [Symfony 5]...
gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Yay, thanks, committed!

gábor hojtsy’s picture

Issue summary: View changes
Issue tags: +8.7.0 release notes

Let's add it to the release notes. Added release notes snippet to issue summary.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kattekrab’s picture

Issue tags: +Needs change record

There'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\TranslatorInterface no longer extends \Symfony\Component\Translation\TranslatorInterface.

gábor hojtsy’s picture

Issue tags: -Needs change record

Created and published change record at https://www.drupal.org/node/3039683