function FieldUiTestTrait::fieldUIAddNewField

Same name in other branches
  1. 9 core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiTestTrait::fieldUIAddNewField()
  2. 8.9.x core/modules/field_ui/src/Tests/FieldUiTestTrait.php \Drupal\field_ui\Tests\FieldUiTestTrait::fieldUIAddNewField()
  3. 8.9.x core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiTestTrait::fieldUIAddNewField()
  4. 11.x core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php \Drupal\Tests\field_ui\Traits\FieldUiTestTrait::fieldUIAddNewField()

Creates a new field through the Field UI.

Parameters

string $bundle_path: Admin path of the bundle that the new field is to be attached to.

string $field_name: The field name of the new field storage.

string $label: (optional) The label of the new field. Defaults to a random string.

string $field_type: (optional) The field type of the new field storage. Defaults to 'test_field'.

array $storage_edit: (optional) $edit parameter for submitForm() on the second step ('Storage settings' form).

array $field_edit: (optional) $edit parameter for submitForm() on the third step ('Field settings' form).

bool $save_settings: (optional) Parameter for conditional execution of second and third step (Saving the storage settings and field settings). Defaults to 'TRUE'.

26 calls to FieldUiTestTrait::fieldUIAddNewField()
CommentNonNodeTest::testCommentFunctionality in core/modules/comment/tests/src/Functional/CommentNonNodeTest.php
Tests anonymous comment functionality.
ContactSitewideTest::testSiteWideContact in core/modules/contact/tests/src/Functional/ContactSitewideTest.php
Tests configuration options and the site-wide contact form.
ContentTranslationSettingsTest::testFieldTranslatableSettingsUI in core/modules/content_translation/tests/src/Functional/ContentTranslationSettingsTest.php
Tests that field setting depends on bundle translatability.
DateTimeTest::testEnteringDateTimeViaSelectors in core/modules/system/tests/src/Functional/System/DateTimeTest.php
Tests handling case with invalid data in selectors (like February, 31st).
EntityReferenceAdminTest::createEntityReferenceField in core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAdminTest.php
Creates a new Entity Reference fields with a given target type.

... See full list

File

core/modules/field_ui/tests/src/Traits/FieldUiTestTrait.php, line 36

Class

FieldUiTestTrait
Provides common functionality for the Field UI test classes.

Namespace

Drupal\Tests\field_ui\Traits

Code

public function fieldUIAddNewField($bundle_path, $field_name, $label = NULL, $field_type = 'test_field', array $storage_edit = [], array $field_edit = [], bool $save_settings = TRUE) {
    // Generate a label containing only letters and numbers to prevent random
    // test failure.
    // See https://www.drupal.org/project/drupal/issues/3030902
    $label = $label ?: $this->randomMachineName();
    $initial_edit = [
        'new_storage_type' => $field_type,
    ];
    $second_edit = [
        'label' => $label,
        'field_name' => $field_name,
    ];
    // Allow the caller to set a NULL path in case they navigated to the right
    // page before calling this method.
    if ($bundle_path !== NULL) {
        $bundle_path = "{$bundle_path}/fields/add-field";
        // First step: 'Add field' page.
        $this->drupalGet($bundle_path);
    }
    else {
        $bundle_path = $this->getUrl();
    }
    try {
        // First check if the passed in field type is not part of a group.
        $this->assertSession()
            ->elementExists('css', "[name='new_storage_type'][value='{$field_type}']");
    } catch (ElementNotFoundException) {
        // Call the helper function to confirm it is in a group.
        $field_group = $this->getFieldFromGroup($field_type);
        if ($field_group) {
            // Pass in the group name as the new storage type.
            $initial_edit['new_storage_type'] = $field_group;
            $second_edit['group_field_options_wrapper'] = $field_type;
            $this->drupalGet($bundle_path);
        }
    }
    $this->submitForm($initial_edit, 'Continue');
    $this->submitForm($second_edit, 'Continue');
    // Assert that the field is not created.
    $this->assertFieldDoesNotExist($bundle_path, $label);
    if ($save_settings) {
        $this->assertSession()
            ->pageTextContains("These settings apply to the {$label} field everywhere it is used.");
        // Test Breadcrumbs.
        $this->getSession()
            ->getPage()
            ->findLink($label);
        // Ensure that each array key in $storage_edit is prefixed with field_storage.
        $prefixed_storage_edit = [];
        foreach ($storage_edit as $key => $value) {
            if (str_starts_with($key, 'field_storage')) {
                $prefixed_storage_edit[$key] = $value;
                continue;
            }
            // If the key starts with settings, it needs to be prefixed differently.
            if (str_starts_with($key, 'settings[')) {
                $prefixed_storage_edit[str_replace('settings[', 'field_storage[subform][settings][', $key)] = $value;
                continue;
            }
            $prefixed_storage_edit['field_storage[subform][' . $key . ']'] = $value;
        }
        // Second step: 'Storage settings' form.
        $this->submitForm($prefixed_storage_edit, 'Update settings');
        // Third step: 'Field settings' form.
        $this->submitForm($field_edit, 'Save settings');
        $this->assertSession()
            ->pageTextContains("Saved {$label} configuration.");
        // Check that the field appears in the overview form.
        $this->assertFieldExistsOnOverview($label);
    }
}

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