Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
... because extending classes should not be allowed to use it, but implement ContainerInjectionInterface for their dependencies.
Comment | File | Size | Author |
---|---|---|---|
#11 | container_base-2110643.patch | 3.51 KB | dawehner |
#5 | 2110643-5.patch | 4.3 KB | amateescu |
#5 | interdiff.txt | 4.07 KB | amateescu |
#1 | 2110643.patch | 1.07 KB | amateescu |
Comments
Comment #1
amateescu CreditAttribution: amateescu commentedLike this. Let's see who's doing it wrong.
We'll also need a good comment there to explain why.
Comment #2
msonnabaum CreditAttribution: msonnabaum commentedYes, please.
This has already caused confusion, and there's a language feature that makes it perfectly clear. Let's use it.
Comment #3
tstoecklerAttempt at a comment:
Comment #5
amateescu CreditAttribution: amateescu commentedSurprisingly few offenders, I guess we must be doing something right.
Comment #6
Crell CreditAttribution: Crell commentedMark asked me to weigh in here. My stance on privates is currently: #2081945-11: [Policy, no patch] Reintroduce 'private' visibility to object-oriented code..
If we do reintroduce privates, then #5 seems a logical place to do it. Although I will note that Symfony's Controller base class has a protected container() method. :-) I think our explanation for doing it differently is that we're trying to educate people about injection, and controllers/forms are the biggest touch-point so it makes more people learn what good injection is.
Comment #7
msonnabaum CreditAttribution: msonnabaum commentedIt's not just that, it's that we want to provide a base class that offers some common services, without making every controller/form container-aware.
This issue came up after a discussion on IRC where there was confusion around whether to inject additional dependencies or to just use the container method that's there. This tells me that we need to clear things up by just making container private.
Comment #8
tim.plunkettSee #2112711: Provide an easier mechanism for using drupal_get_form() directly for a way to clean this up.
Comment #9
tim.plunkettThis is a major issue. Providing the container does not help anyone learn injection, and was not the intention of these classes.
This is best paired with #2110501: ControllerBase should implement ContainerInjectionInterface like FormBase
Comment #10
xjmDiscussed on the phone with @Dries, @webchick, @msonnabaum, @tim.plunkett, and @alexpott -- this should be considered a beta blocker given the DX impact.
Comment #11
dawehnerLet's keep the patch as simple as possible.
Comment #12
damiankloip CreditAttribution: damiankloip commentedSo child classes have to use injection but the base class doesn't, and just calls to \Drupal. This is getting nuts IMO.
Comment #13
damiankloip CreditAttribution: damiankloip commentedSo child classes have to use injection but the base class doesn't, and just calls to \Drupal. People will just never write tests for this stuff. This is getting nuts IMO.
Comment #14
dawehnerIn general controllers should avoid containing logic from my beginning.
Well, this patch at least does not make the situation worse.
When I would have to write my code in contrib I would probably inject everything properly, though things like ->url()/t() aren't really worth to be unit tested. We should maybe provide helper methods on the base unit test, if possible.
Comment #15
dawehnerWell, it certainly does not "improve" the DX, though forcing people to do things better is a good thing.
Comment #16
dawehnerI think this is fine as it is.
Comment #17
xjmDiscussed with @catch; it'd technically be okay to put this in after the beta. (Doesn't affect that this issue is RTBC anyway; just major and beta blocker don't make sense together.) :)
Comment #18
catchI agree with Damian here. Don't particularly object to this, but I think people will just call \Drupal if they're doing it wrong anyway, so leaving for someone else.
Comment #19
amateescu CreditAttribution: amateescu commentedThe alternative would be to admit that all (most?) controllers should be ContainerAware after all? :)
The argumentation for that is that people who want to do things the Right Way will create a service for the main logic of their stuff and test it, so controllers will be tiny glue code, and people who don't care about that or they are just not experienced enough will write "bad" code disregarding all the hurdles we put in place.
Comment #20
webchickI think this makes sense. It makes it so that dependency injection happens consistently in all "user-space" code.
Committed and pushed to 8.x. Thanks!
This was already covered in the change notice at https://drupal.org/node/2168147.
Comment #21
mondrakeLooks like there is a leftover
$this->container()
call in ConfigFormBase?I am getting a
EDIT
Created #2168799: ConfigFormBase fails when invoking $this->container().