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.
While working on #1696648: [META] Untie content entity validation from form validation we needed a place to store our own plugins, but since this isn't a module and we didn't want to store everything inside system, we changed the AnnotatedClassDiscovery so it allows you to specify custom locations to search for.
Attached patch is a proof of concept, and if you want to test it, clear your cache after applying the patch.
How to use inside your own PluginManager
$this->discovery = new AnnotatedClassDiscovery('Validation', 'Constraint');
$this->discovery->custom_location = array(
'Drupal\Core\Validation\Constraint' => array(array(
'dir' => drupal_realpath('core/lib/Drupal/Core/Validation/Constraint'),
'class_base' => 'Drupal\Core\Validation\Constraint',
)),
);
$this->factory = new ConstraintFactory($this->discovery);
Comment | File | Size | Author |
---|---|---|---|
#8 | extended-discovery-7.patch | 3.64 KB | attiks |
#7 | extended-discovery-7.patch | 1.16 KB | attiks |
#3 | extended-discovery-3.patch | 3.67 KB | attiks |
extended-discovery.patch | 3.9 KB | attiks | |
Comments
Comment #1
tim.plunkettNon-modules can already provide plugins.
For example, #1043198: Convert view modes to ConfigEntity adds Drupal\Core\Plugin\Core\Entity\EntityViewMode.
I think this would introduce confusion and uncertainty if Plugins could be *anywhere*.
Comment #2
fagoBut then all plugins what reside below the plugin system - what does not make any sense. It's not the plugin system providing those plugins. For example, we currently have typed data plugins reciding below the typed data system (based upon hook discovery). It are basic implementations provided by the typed data system, so they belong below \Core\TypedData.
Related issue #1821846: Consider better naming conventions for plugin types owned by includes.
Comment #3
attiks CreditAttribution: attiks commentedNew patch using get/set
Comment #5
tstoecklerRe #2: I opened #1828616: AnnotatedClassDiscovery is not finding plugins in Drupal\(Core|Component)\COMPONENT_NAME\Plugin directory exactly because of that weird feeling I got dropping an entity type in \Drupal\Core\Plugin. :-)
Comment #6
attiks CreditAttribution: attiks commentedFor reference, fails on
Fatal error: Class name must be a valid object or a string in /core/includes/entity.inc on line 265
Comment #7
attiks CreditAttribution: attiks commentedComment #8
attiks CreditAttribution: attiks commentedComment #9
yched CreditAttribution: yched commentedCurrently it's easy to see what plugin implementations a module provides, you just look in its lib/[module]/Plugin folder, and easily see which plugins it provides, for which plugin types (say, a field widget, two field formatters, and a view row handler).
Big -1 to losing this if plugin implementations can be "anywhere". Making machine discovery more flexible shouldn't happen at the detriment of easy human discovery.
Comment #10
attiks CreditAttribution: attiks commentedThis isn't for modules but for core components, see #2
Comment #11
attiks CreditAttribution: attiks commentedThe longer answers after a cup of coffee
The idea is that we provide the same experience for non-modules, in the case of TypedData the plugins will be located inside core\lib\Drupal\Core\TypedData so they are easy to find.
You're right that a module in theory could use this mechanism to specify a different location, but I assume module builders will use the default.
Comment #12
tstoecklerI agree with the criticism mentioned here. Maybe we can find a better solution in #1828616: AnnotatedClassDiscovery is not finding plugins in Drupal\(Core|Component)\COMPONENT_NAME\Plugin directory?!
Comment #13
attiks CreditAttribution: attiks commentedI would love to hear alternatives, the problem is not only about the names, but about the discovery process as well. The only problems is that people are now working around this limitation by either using a different discovery process or by putting their plugins inside core/modules/system or inside /Core/Plugin.
Comment #14
attiks CreditAttribution: attiks commentedRelated discussion: #1836008: Remove drupal_classloader() use in Drupal\Core\AnnotatedClassDiscovery
Comment #15
neclimdulI agree with this not being a great idea for the points given. The problem itself also seems mistaken as if its not a module, it should be in the core class structure. This is already supported. The link tstoeckler posted is relevant for how that happens but since it is already possible I'm going to close this.
Comment #16
attiks CreditAttribution: attiks commentedSo where are we supposed to put them, my code lives inside core\lib\Drupal\Core\Validation\Constraint, so do I put my plugins inside core\lib\Drupal\Core\Plugin? Or somewhere else?
I don't mind you closing the issue, but at least give us some feedback on how to handle it
Comment #17
attiks CreditAttribution: attiks commentedComment #18
tstoecklerYup, currently it's Drupal\Core\Plugin. Again, see the referenced issue for figuring out a better place/solution.
Comment #19
attiks CreditAttribution: attiks commented#18 Thanks