Skip to content
Snippets Groups Projects
Commit 38ffd7a1 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1952652 by swentel: Instance components are not removed from entity...

Issue #1952652 by swentel: Instance components are not removed from entity display when the field/instance is deleted.
parent 064c8178
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -226,4 +226,39 @@ public function testRenameDeleteBundle() {
$this->assertFalse($display);
}
/**
* Tests deleting field instance.
*/
public function testDeleteFieldInstance() {
$this->enableModules(array('field_sql_storage', 'field_test'));
// Create a field and an instance.
$field = array(
'field_name' => 'test_field',
'type' => 'test_field'
);
field_create_field($field);
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
);
field_create_instance($instance);
// Create an entity display.
$display = entity_create('entity_display', array(
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'viewMode' => 'default',
))->setComponent($field['field_name'])->save();
// Delete the instance.
$instance = field_info_instance('entity_test', $field['field_name'], 'entity_test');
field_delete_instance($instance);
// Check that the component has been removed from the entity display.
$display = entity_get_display('entity_test', 'entity_test', 'default');
$this->assertFalse($display->getComponent($field['field_name']));
}
}
......@@ -430,6 +430,16 @@ public function delete($field_cleanup = TRUE) {
// hook_field_delete_instance().
$module_handler->invokeAll('field_delete_instance', array($this));
// Remove the instance from the entity displays.
$ids = array();
$view_modes = array('default' => array()) + entity_get_view_modes($this->entity_type);
foreach (array_keys($view_modes) as $view_mode) {
$ids[] = $this->entity_type . '.' . $this->bundle . '.' . $view_mode;
}
foreach (entity_load_multiple('entity_display', $ids) as $display) {
$display->removeComponent($this->field->id())->save();
}
// Delete the field itself if we just deleted its last instance.
if ($field_cleanup && count($this->field->getBundles()) == 0) {
$this->field->delete();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment