Problem/Motivation

The constructor of the Permissions class currently requires the second parameter to be an instance of a TranslationManager class. However, in general, you should aim to program against an interface and not a concrete class (i.e not violating the dependency inversion principle). Because of this, possible other implementations of a TranslationManager (added by other modules) will cause this to fail.

Proposed resolution

In stead of type hinting the concrete class used, we should use an interface as type-hint (following the dependency inversion principle). In this case that would be the TranslationInterface.

Remaining tasks

- Create a patch
- Review patch

User interface changes

None

API changes

See proposed resolution. This should not break existing implementations, as they all will comply to the TranslationInterface. However, this will allow other implementations of the TranslationInterface to be used as well.

Data model changes

See proposed resolution. This should not break existing implementations.

Comments

Dimiter created an issue. See original summary.

dimiter’s picture

Issue summary: View changes
StatusFileSize
new1.37 KB

Actually this patch should be attributed to bonrita! Many thanks!

sandeep date’s picture

Status: Active » Needs review
berdir’s picture

Maybe just use StringTranslationTrait and then use setStringTransalation and $this->t().

dimiter’s picture

@Berdir That might be a good alternative. However, I deliberately kept the changes as small as I possibly could, to prevent unwanted side-effects from being introduced. Perhaps your suggestion could be a new issue on its own?

eelkeblok’s picture

I would also suggest to take this chance and make it more standards-compliant. Although theoretically there might be some derived class somewhere that depends on the protected member $translationManager, chances of that are slim. Other than that, the impact is minimal. Is it possible to deprecate a member variable? In that case you could even deprecate $translationManager but also assign it the injected TranslationInterface, for maximum compatibility.

bonrita’s picture

Issue summary: View changes
bonrita’s picture

Issue summary: View changes
anybody’s picture

Status: Needs review » Needs work

I think #4 is the better solution.

TranslationInterface has no getStorage() method, so IMHO the patch is wrong.

bhupesh_upadhyay’s picture

Hi, I have created a patch, please review this.

bhupesh_upadhyay’s picture

Status: Needs work » Needs review
benstallings’s picture

Category: Bug report » Feature request

The StringTranslationTrait provides getStringTranslation() which falls back to \Drupal::translation() if no translation service has been explicitly set. So the current code already works — $this->t() resolves fine without injection.

For that reason, I'd call this a feature request instead of a bug report.

berdir’s picture

Category: Feature request » Bug report
Priority: Minor » Normal
Status: Needs review » Closed (duplicate)

This used to be a bug, but that was fixed by #3156523: Permissions::__construct is hinting with a class, so closing as duplicate.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.