function EntityFormDisplayTest::testFieldComponent
Same name in other branches
- 9 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testFieldComponent()
- 8.9.x core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testFieldComponent()
- 11.x core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testFieldComponent()
Tests the behavior of a field component within an EntityFormDisplay object.
File
-
core/
modules/ field_ui/ tests/ src/ Kernel/ EntityFormDisplayTest.php, line 70
Class
- EntityFormDisplayTest
- Tests the entity display configuration entities.
Namespace
Drupal\Tests\field_ui\KernelCode
public function testFieldComponent() : void {
// Create a field storage and a field.
$field_name = 'test_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();
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
]);
// Check that providing no options results in default values being used.
$form_display->setComponent($field_name);
$field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->getType());
$default_widget = $field_type_info['default_widget'];
$widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
$expected = [
'weight' => 3,
'type' => $default_widget,
'settings' => $widget_settings,
'third_party_settings' => [],
];
$this->assertEquals($expected, $form_display->getComponent($field_name));
// Check that the getWidget() method returns the correct widget plugin.
$widget = $form_display->getRenderer($field_name);
$this->assertEquals($default_widget, $widget->getPluginId());
$this->assertEquals($widget_settings, $widget->getSettings());
// Check that the widget is statically persisted.
$this->assertSame($widget, $form_display->getRenderer($field_name));
// Check that changing the definition creates a new widget.
$form_display->setComponent($field_name, [
'type' => 'field_test_multiple',
]);
$renderer = $form_display->getRenderer($field_name);
$this->assertEquals('test_field_widget', $renderer->getPluginId());
$this->assertNotSame($widget, $renderer);
// Check that specifying an unknown widget (e.g. case of a disabled module)
// gets stored as is in the display, but results in the default widget being
// used.
$form_display->setComponent($field_name, [
'type' => 'unknown_widget',
]);
$options = $form_display->getComponent($field_name);
$this->assertEquals('unknown_widget', $options['type']);
$widget = $form_display->getRenderer($field_name);
$this->assertEquals($default_widget, $widget->getPluginId());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.