protected function FieldExampleBrowserTestBase::createField in Examples for Developers 3.x
Same name and namespace in other branches
- 8 field_example/tests/src/Functional/FieldExampleBrowserTestBase.php \Drupal\Tests\field_example\Functional\FieldExampleBrowserTestBase::createField()
Create a field on the content type created during setUp().
Parameters
string $type: The storage field type to create.
string $widget_type: The widget to use when editing this field.
int|string $cardinality: Cardinality of the field. Use -1 to signify 'unlimited'.
string $fieldFormatter: The formatter to use when editing this field.
Return value
string Name of the field, like field_something
8 calls to FieldExampleBrowserTestBase::createField()
- ColorBackgroundFormatterTest::testMultiValueField in modules/
field_example/ tests/ src/ Functional/ ColorBackgroundFormatterTest.php - Tests a multi-value field.
- ColorBackgroundFormatterTest::testSingleValueField in modules/
field_example/ tests/ src/ Functional/ ColorBackgroundFormatterTest.php - Field example scenario tests.
- ColorPickerWidgetTest::testMultiValueField in modules/
field_example/ tests/ src/ Functional/ ColorPickerWidgetTest.php - Field example scenario tests.
- ColorPickerWidgetTest::testSingleValueField in modules/
field_example/ tests/ src/ Functional/ ColorPickerWidgetTest.php - Field example scenario tests.
- Text3WidgetTest::testMultiValueField in modules/
field_example/ tests/ src/ Functional/ Text3WidgetTest.php - Test basic functionality of the example field.
File
- modules/
field_example/ tests/ src/ Functional/ FieldExampleBrowserTestBase.php, line 109
Class
- FieldExampleBrowserTestBase
- Class FieldExampleBrowserTestBase.
Namespace
Drupal\Tests\field_example\FunctionalCode
protected function createField($type = 'field_example_rgb', $widget_type = 'field_example_text', $cardinality = '1', $fieldFormatter = 'field_example_simple_text') {
$assert = $this
->assertSession();
$this
->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/fields');
// Go to the 'Add field' page.
$this
->clickLink('Add field');
// Make a name for this field.
$field_name = strtolower($this
->randomMachineName(10));
// Fill out the field form.
$edit = [
'new_storage_type' => $type,
'field_name' => $field_name,
'label' => $field_name,
];
$this
->drupalPostForm(NULL, $edit, 'Save and continue');
// Fill out the $cardinality form as if we're not using an unlimited number
// of values.
$edit = [
'cardinality' => 'number',
'cardinality_number' => (string) $cardinality,
];
// If we have -1 for $cardinality, we should change the form's drop-down
// from 'Number' to 'Unlimited'.
if (-1 == $cardinality) {
$edit = [
'cardinality' => '-1',
'cardinality_number' => '1',
];
}
// And now we save the cardinality settings.
$this
->drupalPostForm(NULL, $edit, 'Save field settings');
$this
->verbose((string) new FormattableMarkup('Saved settings for field %field_name with widget %widget_type and cardinality %cardinality', [
'%field_name' => $field_name,
'%widget_type' => $widget_type,
'%cardinality' => $cardinality,
]));
$assert
->pageTextContains((string) new FormattableMarkup('Updated field @name field settings.', [
'@name' => $field_name,
]));
// Set the widget type for the newly created field.
$this
->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/form-display');
$edit = [
'fields[field_' . $field_name . '][type]' => $widget_type,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
// Set the field formatter for the newly created field.
$this
->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/display');
$edit1 = [
'fields[field_' . $field_name . '][type]' => $fieldFormatter,
];
$this
->drupalPostForm(NULL, $edit1, 'Save');
return $field_name;
}