Problem/Motivation
ElementInfoManager does not provide an alterHook definition. This prevents altering element plugin definitions. As more and more contrib provide elements, sometimes it is required to override them for improvements. This is impossible for element plugins.
See alterDefinitions, where it is skipped if there is no alterHook
protected function alterDefinitions(&$definitions) {
if ($this->alterHook) {
$this->moduleHandler->alter($this->alterHook, $definitions);
}
}
Proposed resolution
Provide an alterHook definition. Invoke \Drupal\Core\Plugin\DefaultPluginManager::alterInfo
in the consturctor so that end users can swap element classes.
Remaining tasks
Add patch.
API changes
Adds a new hook.
Release notes snippet
A new hook has been added to allow altering of render element plugins. Read the change record for more.
Comment | File | Size | Author |
---|---|---|---|
#19 | 2987208-19.patch | 3.7 KB | alexpott |
#19 | 2987208-19.test-only.patch | 1.69 KB | alexpott |
#11 | 2987208-element-plugin-info-alter-11.patch | 1.66 KB | akalata |
#10 | 2987208-element-plugin-info-alter-10.patch | 1.66 KB | akalata |
#8 | 2987208-element-plugin-info-alter-8.patch | 1.58 KB | akalata |
Comments
Comment #2
mglamanTo accomplish this, I swapped the element manager for a custom class:
And then
Comment #3
simePatch to add the alterInfo() call. I've used `form_element_info` to map what a developer would expect it to be called based on @FormElement plugin type but I didn't look deeper.
Comment #5
tim.plunkettBe sure to add a hook example in core/lib/Drupal/Core/Render/theme.api.php, preferably right after
hook_element_info_alter()
.I'd suggest
hook_element_plugin_alter()
here, it's not specific to forms.Comment #6
akalata CreditAttribution: akalata at Tag1 Consulting commentedHere's a quick patch with an attempt at documentation.
Comment #7
tim.plunkettLet's rename this $definitions, seems to be the standard for these hooks.
Great example! :)
There's no need for explicit test coverage here, as it uses the existing
DefaultPluginManager::alterInfo()
method.Comment #8
akalata CreditAttribution: akalata at Tag1 Consulting commentedUpdated based on #7:
$plugins
to$definitions
.Comment #9
tim.plunkettTypo in definitiions.
Also I think this new alter should have a sentence about preferring usage of hook_element_info_alter when possible, since they are slightly different
Comment #10
akalata CreditAttribution: akalata at Tag1 Consulting commentedMore words for #9.
Comment #11
akalata CreditAttribution: akalata at Tag1 Consulting commentedTried editing the patch file manually, missed a step.
Comment #12
tim.plunkettLooks great, thanks!
Comment #14
alexpottI discussed this with @tim.plunkett and we agreed that the API expansion is necessary. This type of thing needs a change record so people know about it and a release notes snippet.
Create a new CR here -> https://www.drupal.org/node/add/changenotice?field_project=3060
Comment #15
Ghost of Drupal PastI hit this issue so often I published a workaround at https://gitlab.com/chx_/do2987208 almost a year ago. I will see what I can do to get this across the finish line.
Comment #16
Ghost of Drupal Pasthttps://www.drupal.org/node/3067713 has been filed.
Comment #17
bircherThis now has a release note snippet and a change notice. The patch is simple and looks good to me.
Comment #18
alexpottIdeally we'd have test coverage of this too. We could add a hook implementation to
element_info_test,module
Comment #19
alexpottThe test-only patch is the interdiff.
Comment #20
alexpottComment #22
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedLooks great :)
Comment #25
larowlanissue credits
Comment #27
larowlanCommitted d68cb07 and pushed to 8.8.x. Thanks!
Published the change record.
thanks folks
Comment #28
ndobromirov CreditAttribution: ndobromirov at FFW commentedJust a late question...
Why introduce new hooks, when there is a viable alternative in D8 - events?
Comment #29
BerdirSee #2237831: Allow module services to specify hooks and tons of related issues and discussions. In short, the current event system has drawbacks and it wouldn't scale to convert all our current hooks to events. And replacing hooks with events has been pretty much put on old. This added a new hook, agreed, but we have a standard system in place for altering plugin definitions and this uses that.