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
When determining the namespaces to set in container.namespaces, which is used for annotated class discovery, DrupalKernel requires that a Plugin directory be present.
This is not true for modules, just for Core and Component.
Proposed resolution
In addition to the hardcoded check for Plugin, add one for Element and Entity.
Since this only affects code in \Drupal\Core and \Drupal\Component, we don't need to worry about extensibility
Remaining tasks
N/A
User interface changes
API changes
N/A
Comment | File | Size | Author |
---|---|---|---|
#18 | 2309889-namespace_dirs-18.patch | 2.44 KB | tim.plunkett |
#18 | interdiff.txt | 793 bytes | tim.plunkett |
#13 | 2309889-namespace_dirs-12.patch | 2.44 KB | tim.plunkett |
#8 | plugin_namespaces-2309889-8.patch | 2.01 KB | dawehner |
#1 | plugin-2309889-1.patch | 772 bytes | tim.plunkett |
Comments
Comment #1
tim.plunkettSee the interdiff in #2305839-18: Convert hook_element_info() to annotated classes
Comment #2
BerdirI did add this to avoid having to parse tons of components folders for nothing. We're talking about 80 folders * dozens of plugin types, that's a lot of file_exists() checks...
Comment #3
sunFor now, let's simply make the condition more complex + add the expected directory names of core plugin types that may exist.
Alternatively, we could as well hard-code a few component names; e.g.,
$component === 'Form' || $component === 'Render'
+ skip theis_dir()
checks altogether. That might make even more sense.Perhaps we should even consider to convert the entire condition to that approach, but separate issue.
Comment #4
dawehnerThere is a little bit documentation above here
Comment #6
dawehnerOne alternative approach could be to just specify the Plugin directories directly in the core.services.yml file, for example.
Comment #7
tim.plunkettNot sure what #6 means.
Any patch should be sure to remove core/lib/Drupal/Core/Render/Plugin/README.txt to prove this works.
Comment #8
dawehnerThis is probably a really bad solution but this should just give some idea.
Comment #9
sunApparently, we're unconditionally adding all namespaces of all modules, regardless of whether they have any plugins. — FWIW, it's a bit pointless to do this optimization for
Component
+Core
, while checking all (possibly a lot more) module namespaces anyway.Thinking through all available options, I think the current implementation is fine. Adding an empty
Plugin
subdirectory isn't really a problem.The only other sensible option would be to fully hard-code the entire list of component/core namespaces, so as to skip the directory scan altogether (because it will always yield the same result anyway).
Comment #11
tim.plunkettI could not disagree more. First, you'd have to know that you need a Plugin directory. Second, you'd need to realize that git doesn't allow empty directories, so you'd add a README.txt like we do in core. But for me, the first step was only possible because I had detailed knowledge of how AnnotatedClassDiscovery and the namespaces were built. And even then, it took me at least an hour to figure out.
I really don't think that's reasonable.
Comment #12
BerdirAs discussed, I'm ok with either just hardcoding a list (but a container parameter seems overkill to me, nobody will add something in there, and if they really do, they could do it in service builder alter or something?) or just extend the existing folder check to include Render and Entity. I don't think we will be adding more top level directories like that?
Comment #13
tim.plunkettTalked with @Berdir some about this. @dawehner's approach looks promising, but we don't need this to change, as this *only* applies to core.
Trying just hardcoding it all.
Comment #14
dawehnermeh okay.
Comment #15
BerdirJust wondering if it's worth to micro-optimize this and talk about the order of checks (e.g. plugin first because that is the most common one?). But OK with this as well.
Comment #16
BerdirDoes need an issue summary update, though.
Comment #17
alexpottI agree #15 we should optimise for the most common use case. Plus yep an issue summary update is needed.
Comment #18
tim.plunkettDid both of those.
Comment #19
tim.plunkettComment #20
alexpottCommitted 305b2f3 and pushed to 8.0.x. Thanks!