diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 8160195a31b..8c591c9a417 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -87,7 +87,11 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn $bundle = $entity->bundle(); // Allow modules to change the form mode. - \Drupal::moduleHandler()->alter('entity_form_mode', $form_mode, $entity); + \Drupal::moduleHandler()->alter( + [$entity_type . '_form_mode', 'entity_form_mode'], + $form_mode, + $entity + ); // Check the existence and status of: // - the display for the form mode, diff --git a/core/lib/Drupal/Core/Entity/entity.api.php b/core/lib/Drupal/Core/Entity/entity.api.php index 816817190fc..7a375e43c20 100644 --- a/core/lib/Drupal/Core/Entity/entity.api.php +++ b/core/lib/Drupal/Core/Entity/entity.api.php @@ -1877,6 +1877,23 @@ function hook_entity_form_mode_alter(&$form_mode, \Drupal\Core\Entity\EntityInte } } +/** + * Change the form mode of a specific entity type currently being displayed. + * + * @param string $form_mode + * The form_mode currently displaying the entity. + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity that is being viewed. + * + * @ingroup entity_crud + */ +function hook_ENTITY_TYPE_form_mode_alter(string &$form_mode, \Drupal\Core\Entity\EntityInterface $entity): void { + // Change the form mode for nodes with 'article' bundle. + if ($entity->bundle() == 'article') { + $form_mode = 'custom_article_form_mode'; + } +} + /** * Alter the settings used for displaying an entity form. * diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 4a6d4ab7144..d4ee147ee84 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -819,3 +819,12 @@ function entity_test_query_entity_test_access_alter(AlterableInterface $query) { $query->condition('entity_test_query_access.name', 'published entity'); } } + +/** + * Implements hook_ENTITY_TYPE_form_mode_alter(). + */ +function entity_test_entity_test_form_mode_alter(&$form_mode, EntityInterface $entity): void { + if ($entity->getEntityTypeId() === 'entity_test' && $entity->get('name')->value === 'test_entity_type_form_mode_alter') { + $form_mode = 'compact'; + } +} diff --git a/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php b/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php index d29cf446ae0..04dd2a5f36c 100644 --- a/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php +++ b/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php @@ -73,9 +73,10 @@ class EntityFormTest extends BrowserTestBase { } /** - * Tests hook_entity_form_mode_alter(). + * Tests hook_entity_form_mode_alter() and hook_ENTITY_TYPE_form_mode_alter(). * * @see entity_test_entity_form_mode_alter() + * @see entity_test_entity_test_form_mode_alter() */ public function testEntityFormModeAlter() { // Create compact entity display. @@ -107,6 +108,13 @@ class EntityFormTest extends BrowserTestBase { $entity2->save(); $this->drupalGet($entity2->toUrl('edit-form')); $this->assertSession()->elementNotExists('css', 'input[name="field_test_text[0][value]"]'); + + $entity3 = EntityTest::create([ + 'name' => 'test_entity_type_form_mode_alter', + ]); + $entity3->save(); + $this->drupalGet($entity3->toUrl('edit-form')); + $this->assertSession()->elementNotExists('css', 'input[name="field_test_text[0][value]"]'); } /**