Problem/Motivation

We use, for instance, NodeType::load('page')->label() to get a bundle's label. But there is also the entity_type.bundle.info, which acts like an entity bundle factory. In the case when hook_entity_bundle_info_alter() alters this label, NodeType::load('page')->label() will return a different value than \Drupal::service('entity_type.bundle.info')->getBundleInfo('node')['page']['label']. This might create confusion at API level as it's not clear which label getter is correct.

Steps to reproduce

A test to prove the bug https://www.drupal.org/pift-ci-job/1905506 (commit 740e84)

Proposed resolution

Deprecate the possibility to alter, in hook_entity_bundle_info_alter(), the label of a bundle defined as config entity. Third party code will have to use other methods to alter the bundle's label in such cases. Depending on the project several methods are available:

  • The simplest way is to change the bundle entity label as is stored in database.
  • Implement hook_entity_load() or hook_ENTITY_TYPE_load().
  • Swap the bundle entity class and implement a custom ::label() method.

Disallow using hook_entity_bundle_info_alter() to alter labels of bundles stored as config entities in Drupal 10.0.0.

Remaining tasks

None.

User interface changes

None.

API changes

Using hook_entity_bundle_info_alter() to alter labels of bundles stored as config entities is deprecated in Drupal 9.2.0.

Data model changes

None.

Release notes snippet

N/A

Issue fork drupal-3186688

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

claudiu.cristea created an issue. See original summary.

claudiu.cristea’s picture

Issue summary: View changes

Adding test for "Steps to reproduce".

claudiu.cristea’s picture

Issue summary: View changes
claudiu.cristea’s picture

Status: Active » Needs review

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.

larowlan’s picture

Priority: Normal » Minor
Issue tags: +Needs reroll, +Bug Smash Initiative

Left a minor comment, but this looks good

We should reference bundle classes as the preferred way to do this

larowlan’s picture

Status: Needs review » Needs work
claudiu.cristea’s picture

Status: Needs work » Needs review

Fixed the deprecation version in code & CR

larowlan’s picture

Left another comment, could be micro-optimization, but I think it's worth it

claudiu.cristea’s picture

Issue tags: -Needs reroll
  • Added documentation to hook_entity_bundle_info_alter().
  • Restarted tests as it seems that the last failure is random.
  • Removed "Needs reroll" tag.
larowlan’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @claudiu.cristea, this looks good to me

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.

catch’s picture

Status: Reviewed & tested by the community » Needs work

I'm not sure about this change.

First of all I saw the 'change to an exception in 10.0' comment, and that made me realise this code would have to stick around permanently. Then I thought about whether we could just not fire the info hook for config bundle entities at all, but it's callled on the whole info array not per-bundle so that's no good either.

I think instead of adding quite a lot of code to enforce this, we should improve the documentation of hook_entity_bundle_info_alter() instead to mention that config entities shouldn't be altered in there.

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.

Rassoni made their first commit to this issue’s fork.

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.