function EntityFormTest::testEntityFormModeAlter

Same name in other branches
  1. 10 core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest::testEntityFormModeAlter()
  2. 11.x core/modules/system/tests/src/Functional/Entity/EntityFormTest.php \Drupal\Tests\system\Functional\Entity\EntityFormTest::testEntityFormModeAlter()

Tests hook_entity_form_mode_alter().

See also

entity_test_entity_form_mode_alter()

File

core/modules/system/tests/src/Functional/Entity/EntityFormTest.php, line 71

Class

EntityFormTest
Tests the entity form.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testEntityFormModeAlter() {
    // Create compact entity display.
    EntityFormMode::create([
        'id' => 'entity_test.compact',
        'targetEntityType' => 'entity_test',
    ])->save();
    EntityFormDisplay::create([
        'targetEntityType' => 'entity_test',
        'bundle' => 'entity_test',
        'mode' => 'compact',
        'status' => TRUE,
    ])->removeComponent('field_test_text')
        ->save();
    // The field should be available on default form mode.
    $entity1 = EntityTest::create([
        'name' => $this->randomString(),
    ]);
    $entity1->save();
    $this->drupalGet($entity1->toUrl('edit-form'));
    $this->assertSession()
        ->elementExists('css', 'input[name="field_test_text[0][value]"]');
    // The field should be hidden on compact form mode.
    // See: entity_test_entity_form_mode_alter().
    $entity2 = EntityTest::create([
        'name' => 'compact_form_mode',
    ]);
    $entity2->save();
    $this->drupalGet($entity2->toUrl('edit-form'));
    $this->assertSession()
        ->elementNotExists('css', 'input[name="field_test_text[0][value]"]');
}

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