function FieldAttachOtherTest::testEntityFormDisplayBuildForm
Same name in other branches
- 9 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayBuildForm()
- 8.9.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayBuildForm()
- 11.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayBuildForm()
Tests \Drupal\Core\Entity\Display\EntityFormDisplayInterface::buildForm().
This could be much more thorough, but it does verify that the correct widgets show up.
File
-
core/
modules/ field/ tests/ src/ Kernel/ FieldAttachOtherTest.php, line 251
Class
- FieldAttachOtherTest
- Tests other Field API functions.
Namespace
Drupal\Tests\field\KernelCode
public function testEntityFormDisplayBuildForm() : void {
$this->createFieldWithStorage('_2');
$entity_type = 'entity_test';
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)
->create([
'id' => 1,
'revision_id' => 1,
'type' => $this->fieldTestData->field
->getTargetBundle(),
]);
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Test generating widgets for all fields.
$display = $display_repository->getFormDisplay($entity_type, $this->fieldTestData->field
->getTargetBundle());
$form = [];
$form_state = new FormState();
$display->buildForm($entity, $form, $form_state);
$this->assertEquals($this->fieldTestData->field
->getLabel(), $form[$this->fieldTestData->field_name]['widget']['#title'], "First field's form title is {$this->fieldTestData->field->getLabel()}");
$this->assertEquals($this->fieldTestData->field_2
->getLabel(), $form[$this->fieldTestData->field_name_2]['widget']['#title'], "Second field's form title is {$this->fieldTestData->field_2->getLabel()}");
for ($delta = 0; $delta < $this->fieldTestData->field_storage
->getCardinality(); $delta++) {
// field_test_widget uses 'textfield'
$this->assertEquals('textfield', $form[$this->fieldTestData->field_name]['widget'][$delta]['value']['#type'], "First field's form delta {$delta} widget is textfield");
}
for ($delta = 0; $delta < $this->fieldTestData->field_storage_2
->getCardinality(); $delta++) {
// field_test_widget uses 'textfield'
$this->assertEquals('textfield', $form[$this->fieldTestData->field_name_2]['widget'][$delta]['value']['#type'], "Second field's form delta {$delta} widget is textfield");
}
// Test generating widgets for all fields.
$display = $display_repository->getFormDisplay($entity_type, $this->fieldTestData->field
->getTargetBundle());
foreach ($display->getComponents() as $name => $options) {
if ($name != $this->fieldTestData->field_name_2) {
$display->removeComponent($name);
}
}
$form = [];
$form_state = new FormState();
$display->buildForm($entity, $form, $form_state);
$this->assertFalse(isset($form[$this->fieldTestData->field_name]), 'The first field does not exist in the form');
$this->assertEquals($this->fieldTestData->field_2
->getLabel(), $form[$this->fieldTestData->field_name_2]['widget']['#title'], "Second field's form title is {$this->fieldTestData->field_2->getLabel()}");
for ($delta = 0; $delta < $this->fieldTestData->field_storage_2
->getCardinality(); $delta++) {
// field_test_widget uses 'textfield'
$this->assertEquals('textfield', $form[$this->fieldTestData->field_name_2]['widget'][$delta]['value']['#type'], "Second field's form delta {$delta} widget is textfield");
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.