function EntityFormDisplayTest::testDeleteField

Same name and namespace in other branches
  1. 9 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testDeleteField()
  2. 10 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testDeleteField()
  3. 11.x core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testDeleteField()

Tests deleting field.

File

core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php, line 194

Class

EntityFormDisplayTest
Tests the entity display configuration entities.

Namespace

Drupal\Tests\field_ui\Kernel

Code

public function testDeleteField() {
    $field_name = 'test_field';
    // Create a field storage and a field.
    $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test',
        'type' => 'test_field',
    ]);
    $field_storage->save();
    $field = FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => 'entity_test',
    ]);
    $field->save();
    // Create default and compact entity display.
    EntityFormMode::create([
        'id' => 'entity_test.compact',
        'targetEntityType' => 'entity_test',
    ])->save();
    EntityFormDisplay::create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'default',
    ])->setComponent($field_name)
        ->save();
    EntityFormDisplay::create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'compact',
    ])->setComponent($field_name)
        ->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    // Check the component exists.
    $display = $display_repository->getFormDisplay('entity_test', 'entity_test');
    $this->assertNotEmpty($display->getComponent($field_name));
    $display = $display_repository->getFormDisplay('entity_test', 'entity_test', 'compact');
    $this->assertNotEmpty($display->getComponent($field_name));
    // Delete the field.
    $field->delete();
    // Check that the component has been removed from the entity displays.
    $display = $display_repository->getFormDisplay('entity_test', 'entity_test');
    $this->assertNull($display->getComponent($field_name));
    $display = $display_repository->getFormDisplay('entity_test', 'entity_test', 'compact');
    $this->assertNull($display->getComponent($field_name));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.