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.
Problem/Motivation
Follow-up from #2594425: Add the option in system menu block to "Expand all items in this tree" to make new constructor argument dependency optional
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#13 | 3018863-13.patch | 2.79 KB | alexpott |
#13 | 12-13-interdiff.txt | 1 KB | alexpott |
#12 | 3018863-12.patch | 2.79 KB | alexpott |
#12 | 4-12-interdiff.txt | 1.49 KB | alexpott |
#4 | 3018863-3.patch | 1.3 KB | joelpittet |
Comments
Comment #2
joelpittetHere's a patch to make the argument optional and still get the service dependency.
Comment #3
BerdirYou could also do it like this:
$this->menuActiveTrail = $menu_active_trail ?: \Drupal::service('menu.active_trail');
But then you can't do a @trigger_error(), which we might want to add as well.
Comment #4
joelpittetAdded a error for E_USER_DEPRECATED
Comment #6
BerdirGreat.
Not sure if we need the change record referenced, since the necessary change is obvious and the change record does actually not explain it further anyway. We have a rule to always have it, but seems strange for those trivial constructor changes.
Comment #7
andypostRTBC++ to get this change in, the list of contrib affected in related issue
Comment #8
Sam152 CreditAttribution: Sam152 commentedI didn't think constructors or plugins were part of the API, but perhaps this should be a special case if it's extended a lot in contrib.
For these kind of changes, do we require the code path be triggered at least once in a test?
Comment #9
joelpittethttps://www.drupal.org/core/d8-bc-policy
Technically they are @internal implicitly by the BC policy.
I'm trying to fix in contrib, but just for the potential disruptive nature of them, it would be nice to the new things optional.
#2968049-19: Dependency Injection issue
Comment #10
alexpottThe thing is this work then needs to be undone sometime. Yes the @trigger_error() makes it easier to find but there's even less disruption if people extend plugins doing some thing like https://www.previousnext.com.au/blog/safely-extending-drupal-8-plugin-cl...
For me, contrib / custom doing something like:
Comment #11
Berdir@alexpott: Interesting, I think I like that approach. But nobody is using that yet, including core.
What about getting this in, because there are contrib modules out there that are now broken in 8.7 and then creating a documentation page on backwards and forwards compatible ways to subclass services as well as plugins/controllers/.. and start doing that in core?
For services, setter-based injection works well, which I've documented a while ago here: https://www.md-systems.ch/de/blog/techblog/2016/12/17/how-to-safely-inje....
Comment #12
alexpottSo I think to be friendly we should do this patch. In future I think we should seriously consider enforcing the non-extendability of plugin classes by using final so that people are force to use composition over inheritance to extend from core. This will solve lots of these type of issues and, whilst it will cost more upfront, will put the Drupal eco-system in a more maintainable and upgradable state.
On of the problems of doing this is that we're introducing logic that we need to add a test for. Here's one.
Comment #13
alexpottUghs meant to make the operator
===
instead of==
Comment #14
alexpottJust opened #3019332: Use final to define classes that are NOT extension points which was inspired by this and other issues.
Comment #15
alexpottCommitted 449e2e3 and pushed to 8.7.x. Thanks!
Comment #17
jibranLee posted one as well https://www.previousnext.com.au/blog/safely-extending-drupal-8-plugin-cl...Don't mind me @alexpott shared it already in #10.