function EntityFormTest::testEntityFormModeAlter

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

Tests hook_entity_form_mode_alter() and hook_ENTITY_TYPE_form_mode_alter().

See also

entity_test_entity_form_mode_alter()

entity_test_entity_test_form_mode_alter()

File

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

Class

EntityFormTest
Tests the entity form.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testEntityFormModeAlter() : void {
    // Create compact entity display.
    EntityFormMode::create([
        'id' => 'entity_test.compact',
        'label' => '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]"]');
    $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]"]');
}

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