Problem/Motivation
Since #2332935: Allow code to respond to entity/field schema changes entity type and field CRUD events are dispatched. There's no reason why not to implement this for bundle operations: create, delete. Right now, reacting to bundle CRUD events is done by implementing hook_entity_bundle_create()
and hook_entity_bundle_delete()
. In 9.0.x we want to switch to a modern event dispatch/subscribe model but that needs a deprecation step. The 8.1.x is a good point to introduce the new dispatcher that can live together with the deprecated hook dispatcher to assure BC and the transition.
Proposed resolution
Dispatch bundle CRUD events by:
- Define bundle event constants in a new
BundleEvents
final class (I agree, I don't understand why these classes are 'final', maybe we shoul document somewhere this). - Create a new
BundleEvent
event class by extendingSymfony\Component\EventDispatcher\GenericEvent
. Inject the bundle entity type ID and the entity type ID in constructor. - Dispatch events under
EntityTypeManagerInterface
:onBundleCreate()
,onBundleDelete()
. - Deprecate corresponding hooks but keep them until 9.0.x.
- Convert one case of 'create' and one case of 'delete': picked up
field_ui_entity_bundle_create()
andlanguage_entity_bundle_delete()
. - Open a 8.1.x follow-up to convert the remaining hooks implementation from core and core modules.
Remaining tasks
Open a issue for 9.0.x to remove the hook dispatching.
User interface changes
None.
API changes
Core and modules can react to bundle CRUD operations by implementing event subscribers.
Data model changes
None.
Beta phase evaluation
Issue category | Feature because allows also the core to subscribe to bundle CRUD events. |
---|---|
Issue priority | Not critical because right now we have hook implementations in place and we still can react from modules to those events. |
Disruption | No disruption. |
Comment | File | Size | Author |
---|---|---|---|
#29 | reroll_diff_2553169_19-29.txt | 10.99 KB | ankithashetty |
#29 | 2553169-29.patch | 18.6 KB | ankithashetty |
#19 | interdiff.txt | 4.55 KB | claudiu.cristea |
#19 | 2553169-19.patch | 18.32 KB | claudiu.cristea |
Comments
Comment #2
yched CreditAttribution: yched commentedNot sure I get this - since we have hooks, why do we *need* events ?
I mean, events are cool, but adding them *in addition to the existing hooks* sounds like it belongs to #2551893: Add events for matching entity hooks ?
Comment #3
claudiu.cristea@yched,
Maybe I over-used the word *need* and for sure this is not critical. Course we have and we can use hooks. The problem is with those objects that we are moving to core (like ER). I know that the System module can hold those hook implementations in behalf of core but this is not the correct solution. It's only the "no other option" solution. If Symfony events are not more expensive than hooks (this I don't know), then it would be better to move to events. They will be in addition to hooks only for BC reasons and hooks can be removed in D9.
For me #2551893: Add events for matching entity hooks looks more like dealing with content entities (equivalent of hook_entity_HOOK() and hook_ENTITY_ID_HOOK()).
Comment #4
claudiu.cristeaAdded beta evaluation.
Comment #5
claudiu.cristeaComment #6
claudiu.cristeaWe are somehow stuck in #1978714: Entity reference doesn't update its field settings when referenced entity bundles are deleted because we cannot find a "home" for
getFieldsReferencing()
new method. If this goes in, it would unblock the other issue because we'll be able to react on bundle rename by adding an event subscriber and that will hold alsogetFieldsReferencing()
.Comment #7
claudiu.cristeaI still think this should go before 9.0.x.
Comment #9
claudiu.cristeaReworked after EntityManager split and remove of entity type bundle renaming.
Comment #11
claudiu.cristeaComment #12
claudiu.cristeaUpdated IS.
Comment #13
claudiu.cristeaHere I added 2 use-cases from filed_ui and language modules.
Comment #17
claudiu.cristeaRerolled for 8.3.x.
Comment #18
dawehnerIs there a reason we cannot inject the dependency?
Compareable its just sad how much more code one need for a simple subscriber
Do you mind to use 'public' here?
Comment #19
claudiu.cristeaHere's with dependency injection.
Comment #28
longwaveDiscovered via #3203147: Update @todo comment in EntityBundleListenerInterface
Somewhat unsurprisingly #19 no longer applies.
Comment #29
ankithashettyRerolled the patch in #19, thanks!
Comment #30
claudiu.cristeaThis issue is severely outdated.
We need 2 events, BundleCreateEvent & BundleDeleteEvent, even they totally extend a BundleEventBase.
Symfony is about to standardize usage of the event full qualified class name instead of these constants. Using event constants only creates problems, see one of them: #2825358-25: Event class constants for event names can cause fatal errors when a module is installed
The new param should default to
NULL
, for BC reasons. Also we have to test if is missed and trigger a deprecation error. See other service argument additions.This comment should be removed. We have an official way to deprecate.
We should use the
::invokeAllDeprecated()
method that allows to deprecate hooks. See https://www.drupal.org/about/core/policies/core-change-policies/drupal-c.... We need also to provide a test to prove that the deprecation error is triggered. tagging as Needs tests. We need also a change notice. Tagging.This should be updated as versions and also as message pattern.
As this is new code, we can add return strict typing.
On
::storeState()
also the$event_name
.This is a core test and it's not supposed to "know" that Node module exist. Let's use EntityTestBundle bundle config entity as
entity_test
is already installed in this test.