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 extending Symfony\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() and language_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

Reference: https://www.drupal.org/core/beta-changes
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.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

claudiu.cristea created an issue. See original summary.

yched’s picture

Not 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 ?

claudiu.cristea’s picture

Status: Active » Needs review
FileSize
13.5 KB

@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()).

claudiu.cristea’s picture

Issue summary: View changes

Added beta evaluation.

claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

We 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 also getFieldsReferencing().

claudiu.cristea’s picture

Version: 8.0.x-dev » 8.1.x-dev

I still think this should go before 9.0.x.

Status: Needs review » Needs work

The last submitted patch, 3: dispatch_bundle_crud-2553169-3.patch, failed testing.

claudiu.cristea’s picture

Status: Needs review » Needs work

The last submitted patch, 9: dispatch_bundle_crud-2553169-9.patch, failed testing.

claudiu.cristea’s picture

Status: Needs work » Needs review
FileSize
11.65 KB
845 bytes
claudiu.cristea’s picture

Issue summary: View changes

Updated IS.

claudiu.cristea’s picture

Here I added 2 use-cases from filed_ui and language modules.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Status: Needs review » Needs work

The last submitted patch, 13: dispatch_bundle_crud-2553169-13.patch, failed testing.

claudiu.cristea’s picture

Rerolled for 8.3.x.

dawehner’s picture

  1. +++ b/core/lib/Drupal/Core/Entity/EntityBundleListener.php
    @@ -81,8 +91,29 @@ public function onBundleDelete($bundle, $entity_type_id) {
    +  /**
    +   * Returns the event dispatcher service.
    +   *
    +   * @internal
    +   *
    +   * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
    +   *   The event dispatcher service.
    +   *
    +   * @todo Properly inject the event dispatcher service in Drupal 9.0.x.
    +   */
    +  protected function getEventDispatcher() {
    +    if (!isset($this->eventDispatcher)) {
    +      $this->eventDispatcher = \Drupal::service('event_dispatcher');
    +    }
    +    return $this->eventDispatcher;
    +  }
    

    Is there a reason we cannot inject the dependency?

  2. +++ b/core/modules/field_ui/src/EventSubscriber/FieldUiBundleEventSubscriber.php
    @@ -0,0 +1,52 @@
    +class FieldUiBundleEventSubscriber implements EventSubscriberInterface {
    

    Compareable its just sad how much more code one need for a simple subscriber

  3. +++ b/core/modules/field_ui/src/EventSubscriber/FieldUiBundleEventSubscriber.php
    @@ -0,0 +1,52 @@
    +  static function getSubscribedEvents() {
    

    Do you mind to use 'public' here?

claudiu.cristea’s picture

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

longwave’s picture

Status: Needs review » Needs work

Discovered via #3203147: Update @todo comment in EntityBundleListenerInterface

Somewhat unsurprisingly #19 no longer applies.

ankithashetty’s picture

Status: Needs work » Needs review
FileSize
18.6 KB
10.99 KB

Rerolled the patch in #19, thanks!

claudiu.cristea’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs change record

This issue is severely outdated.

  1. +++ b/core/lib/Drupal/Core/Entity/BundleEvent.php
    @@ -0,0 +1,59 @@
    +class BundleEvent extends GenericEvent {
    

    We need 2 events, BundleCreateEvent & BundleDeleteEvent, even they totally extend a BundleEventBase.

  2. +++ b/core/lib/Drupal/Core/Entity/BundleEvents.php
    @@ -0,0 +1,37 @@
    +final class BundleEvents {
    ...
    +  const CREATE = 'entity_type.bundle.create';
    ...
    +  const DELETE = 'entity_type.bundle.delete';
    

    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

  3. +++ b/core/lib/Drupal/Core/Entity/EntityBundleListener.php
    @@ -48,12 +56,15 @@ class EntityBundleListener implements EntityBundleListenerInterface {
    +   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
    +   *   The event dispatcher service.
    ...
    +  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityFieldManagerInterface $entity_field_manager, ModuleHandlerInterface $module_handler, EventDispatcherInterface $event_dispatcher) {
    ...
    +    $this->eventDispatcher = $event_dispatcher;
    

    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.

  4. +++ b/core/lib/Drupal/Core/Entity/EntityBundleListener.php
    @@ -81,7 +95,11 @@ public function onBundleDelete($bundle, $entity_type_id) {
    +    // @todo Remove the hook invocation in Drupal 9.0.x.
    

    This comment should be removed. We have an official way to deprecate.

  5. +++ b/core/lib/Drupal/Core/Entity/EntityBundleListener.php
    @@ -81,7 +95,11 @@ public function onBundleDelete($bundle, $entity_type_id) {
         $this->moduleHandler->invokeAll('entity_bundle_delete', [$entity_type_id, $bundle]);
    

    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.

  6. +++ b/core/lib/Drupal/Core/Entity/entity.api.php
    @@ -748,6 +748,11 @@ function hook_entity_bundle_info_alter(&$bundles) {
    + * @deprecated in Drupal 8.3.x, will be removed in Drupal 9.0.x. Implement
    + *   \Symfony\Component\EventDispatcher\EventSubscriberInterface and respond to
    + *   \Drupal\Core\Entity\BundleEvents::CREATE event in order to react to bundle
    + *   creation.
    
    @@ -766,6 +771,11 @@ function hook_entity_bundle_create($entity_type_id, $bundle) {
    + * @deprecated in Drupal 8.3.x, will be removed in Drupal 9.0.x. Implement
    + *   \Symfony\Component\EventDispatcher\EventSubscriberInterface and respond to
    + *   \Drupal\Core\Entity\BundleEvents::DELETE event in order to react to bundle
    + *   deletion.
    

    This should be updated as versions and also as message pattern.

  7. +++ b/core/modules/field_ui/src/EventSubscriber/FieldUiBundleEventSubscriber.php
    @@ -0,0 +1,52 @@
    +  public static function getSubscribedEvents() {
    ...
    +  public function onBundleCreate(BundleEvent $event) {
    
    +++ b/core/modules/language/src/EventSubscriber/LanguageBundleEventSubscriber.php
    @@ -0,0 +1,35 @@
    +  static function getSubscribedEvents() {
    ...
    +  public function onBundleDelete(BundleEvent $event) {
    
    +++ b/core/modules/system/tests/modules/entity_test/src/EntityTestBundleSubscriber.php
    @@ -0,0 +1,91 @@
    +  public static function getSubscribedEvents() {
    ...
    +  public function onBundleCreate(BundleEvent $event, $event_name) {
    ...
    +  public function onBundleDelete(BundleEvent $event, $event_name) {
    ...
    +  protected function storeState(BundleEvent $event, $event_name) {
    
    +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php
    @@ -807,4 +811,31 @@ function testLongNameFieldIndexes() {
    +  public function testBundleEvents() {
    

    As this is new code, we can add return strict typing.

    On ::storeState() also the $event_name.

  8. +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php
    @@ -807,4 +811,31 @@ function testLongNameFieldIndexes() {
    +    $this->enableModules(['node']);
    +    $this->installEntitySchema('node');
    

    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.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.