public function FieldAttachOtherTest::testEntityFormDisplayBuildForm in Drupal 8
Same name and namespace in other branches
- 9 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayBuildForm()
- 10 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 248
Class
- FieldAttachOtherTest
- Tests other Field API functions.
Namespace
Drupal\Tests\field\KernelCode
public function testEntityFormDisplayBuildForm() {
$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
->assertEqual($form[$this->fieldTestData->field_name]['widget']['#title'], $this->fieldTestData->field
->getLabel(), "First field's form title is {$this->fieldTestData->field->getLabel()}");
$this
->assertEqual($form[$this->fieldTestData->field_name_2]['widget']['#title'], $this->fieldTestData->field_2
->getLabel(), "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
->assertEqual($form[$this->fieldTestData->field_name]['widget'][$delta]['value']['#type'], 'textfield', "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
->assertEqual($form[$this->fieldTestData->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "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
->assertEqual($form[$this->fieldTestData->field_name_2]['widget']['#title'], $this->fieldTestData->field_2
->getLabel(), "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
->assertEqual($form[$this->fieldTestData->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "Second field's form delta {$delta} widget is textfield");
}
}