Problem/Motivation

Split off as a @todo for an obscure edge case of #2570593: Allow entities to be subclassed using "bundle classes"

At https://git.drupalcode.org/project/drupal/-/merge_requests/200#note_14489 @alexpott asks:

What happens if the preCreate sets a bundle that is altered to use a class. That's not going to work - right?

@berdir adds:

[I]t does mean that you couldn't provide a bundle class for the default bundle, which might or might not be fine. We could call getBundleFromValues() a second time after calling preCreate(), there's still an edge case where that class would implement preCreate() too, but we can't have everything? But minor, fine with me if we skip that.

And @larowlan asks:

could we throw an exception if the bundle was non empty in $values but changed in preCreate?

Steps to reproduce

TBD. I think:

  1. Install a version of Drupal that includes #2570593: Allow entities to be subclassed using "bundle classes" (or apply the latest patch there).
  2. Register a bundle class for 'article' nodes.
  3. Define a preCreate() method in the bundle class.
  4. Swap out the bundle you're dealing with.
  5. Notice that you can't change which class is registered as the bundle class this way.

Proposed resolution

TBD

Remaining tasks

  1. Decide what to do.
  2. Do it.
  3. Write / update tests.
  4. Reviews / refinements.
  5. RTBC.

User interface changes

API changes

Data model changes

Release notes snippet

Comments

dww created an issue. See original summary.

dww’s picture

Title: [PP-1] Decide what to » [PP-1] Decide what to do in ContentEntityStorageBase::create() if preCreate() tries to change the bundle
larowlan’s picture

Title: [PP-1] Decide what to do in ContentEntityStorageBase::create() if preCreate() tries to change the bundle » Decide what to do in ContentEntityStorageBase::create() if preCreate() tries to change the bundle
Status: Postponed » Active

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.