diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php index 637489b..c8f38f7 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityDisplayTest.php @@ -257,19 +257,32 @@ public function testDeleteFieldInstance() { )); $instance->save(); - // Create an entity display. + // Create default and teaser entity display. entity_create('entity_display', array( 'targetEntityType' => 'entity_test', 'bundle' => 'entity_test', - 'viewMode' => 'default', + 'mode' => 'default', + ))->setComponent($field_name)->save(); + entity_create('entity_display', array( + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'mode' => 'teaser', ))->setComponent($field_name)->save(); + // Check the component exists. + $display = entity_get_display('entity_test', 'entity_test', 'default'); + $this->assertTrue($display->getComponent($field_name)); + $display = entity_get_display('entity_test', 'entity_test', 'teaser'); + $this->assertTrue($display->getComponent($field_name)); + // Delete the instance. $instance->delete(); - // Check that the component has been removed from the entity display. + // Check that the component has been removed from the entity displays. $display = entity_get_display('entity_test', 'entity_test', 'default'); $this->assertFalse($display->getComponent($field_name)); + $display = entity_get_display('entity_test', 'entity_test', 'teaser'); + $this->assertFalse($display->getComponent($field_name)); } } diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php index 595ac74..79d2bf0 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityFormDisplayTest.php @@ -120,4 +120,52 @@ public function testFieldComponent() { $this->assertEqual($widget->getPluginId(), $default_widget); } + /** + * Tests deleting field instance. + */ + public function testDeleteFieldInstance() { + $this->enableModules(array('field_sql_storage', 'field_test')); + + $field_name = 'test_field'; + // Create a field and an instance. + $field = entity_create('field_entity', array( + 'field_name' => $field_name, + 'type' => 'test_field' + )); + $field->save(); + $instance = entity_create('field_instance', array( + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'bundle' => 'entity_test', + )); + $instance->save(); + + // Create default and compact entity display. + entity_create('entity_form_display', array( + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'mode' => 'default', + ))->setComponent($field_name)->save(); + entity_create('entity_form_display', array( + 'targetEntityType' => 'entity_test', + 'bundle' => 'entity_test', + 'mode' => 'compact', + ))->setComponent($field_name)->save(); + + // Check the component exists. + $display = entity_get_form_display('entity_test', 'entity_test', 'default'); + $this->assertTrue($display->getComponent($field_name)); + $display = entity_get_form_display('entity_test', 'entity_test', 'compact'); + $this->assertTrue($display->getComponent($field_name)); + + // Delete the instance. + $instance->delete(); + + // Check that the component has been removed from the entity displays. + $display = entity_get_form_display('entity_test', 'entity_test', 'default'); + $this->assertFalse($display->getComponent($field_name)); + $display = entity_get_form_display('entity_test', 'entity_test', 'compact'); + $this->assertFalse($display->getComponent($field_name)); + } + } diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index eaa0c70..8f391ad 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -458,7 +458,12 @@ public function delete($field_cleanup = TRUE) { field_cache_clear(); // Remove the instance from the entity form displays. - if ($form_display = entity_load('entity_form_display', $this->entity_type . '.' . $this->bundle . '.default')) { + $ids = array(); + $form_modes = array('default' => array()) + entity_get_form_modes($this->entity_type); + foreach (array_keys($form_modes) as $form_mode) { + $ids[] = $this->entity_type . '.' . $this->bundle . '.' . $form_mode; + } + foreach (entity_load_multiple('entity_form_display', $ids) as $form_display) { $form_display->removeComponent($this->field->name)->save(); } 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 b401470..154bfb8 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -159,6 +159,23 @@ function entity_test_entity_view_mode_info_alter(&$view_modes) { } /** + * Implements hook_entity_form_mode_info_alter(). + */ +function entity_test_entity_form_mode_info_alter(&$form_modes) { + $entity_info = entity_get_info(); + foreach ($entity_info as $entity_type => $info) { + if ($entity_info[$entity_type]['module'] == 'entity_test') { + $form_modes[$entity_type] = array( + 'compact' => array( + 'label' => t('Compact version'), + 'status' => TRUE, + ), + ); + } + } +} + +/** * Implements hook_field_extra_fields(). */ function entity_test_field_extra_fields() {