function EntityReferenceAutoCreateTest::setUp

Same name in other branches
  1. 8.9.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::setUp()
  2. 10 core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::setUp()
  3. 11.x core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php \Drupal\Tests\field\Functional\EntityReference\EntityReferenceAutoCreateTest::setUp()

Overrides BrowserTestBase::setUp

File

core/modules/field/tests/src/Functional/EntityReference/EntityReferenceAutoCreateTest.php, line 46

Class

EntityReferenceAutoCreateTest
Tests creating new entity (e.g. taxonomy-term) from an autocomplete widget.

Namespace

Drupal\Tests\field\Functional\EntityReference

Code

protected function setUp() : void {
    parent::setUp();
    // Create "referencing" and "referenced" node types.
    $referencing = $this->drupalCreateContentType();
    $this->referencingType = $referencing->id();
    $referenced = $this->drupalCreateContentType();
    $this->referencedType = $referenced->id();
    FieldStorageConfig::create([
        'field_name' => 'test_field',
        'entity_type' => 'node',
        'translatable' => FALSE,
        'entity_types' => [],
        'settings' => [
            'target_type' => 'node',
        ],
        'type' => 'entity_reference',
        'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
    ])->save();
    FieldConfig::create([
        'label' => 'Entity reference field',
        'field_name' => 'test_field',
        'entity_type' => 'node',
        'bundle' => $referencing->id(),
        'settings' => [
            'handler' => 'default',
            'handler_settings' => [
                // Reference a single vocabulary.
'target_bundles' => [
                    $referenced->id(),
                ],
                // Enable auto-create.
'auto_create' => TRUE,
            ],
        ],
    ])
        ->save();
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getViewDisplay('node', $referencing->id())
        ->setComponent('test_field')
        ->save();
    $display_repository->getFormDisplay('node', $referencing->id(), 'default')
        ->setComponent('test_field', [
        'type' => 'entity_reference_autocomplete',
    ])
        ->save();
    $account = $this->drupalCreateUser([
        'access content',
        "create {$this->referencingType} content",
    ]);
    $this->drupalLogin($account);
}

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