function TaggedWithTest::setUp

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::setUp()
  2. 8.9.x core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::setUp()
  3. 10 core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php \Drupal\Tests\views\Functional\Wizard\TaggedWithTest::setUp()

Overrides WizardTestBase::setUp

File

core/modules/views/tests/src/Functional/Wizard/TaggedWithTest.php, line 79

Class

TaggedWithTest
Tests the ability of the views wizard to create views filtered by taxonomy.

Namespace

Drupal\Tests\views\Functional\Wizard

Code

protected function setUp($import_test_views = TRUE, $modules = []) : void {
    parent::setUp($import_test_views, $modules);
    // Create two content types. One will have an autocomplete tagging field,
    // and one won't.
    $this->nodeTypeWithTags = $this->drupalCreateContentType();
    $this->nodeTypeWithoutTags = $this->drupalCreateContentType();
    // Create the vocabulary for the tag field.
    $this->tagVocabulary = Vocabulary::create([
        'name' => 'Views testing tags',
        'vid' => 'views_testing_tags',
    ]);
    $this->tagVocabulary
        ->save();
    // Create the tag field itself.
    $this->tagFieldName = 'field_views_testing_tags';
    $handler_settings = [
        'target_bundles' => [
            $this->tagVocabulary
                ->id() => $this->tagVocabulary
                ->id(),
        ],
        'auto_create' => TRUE,
    ];
    $this->createEntityReferenceField('node', $this->nodeTypeWithTags
        ->id(), $this->tagFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
    
    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');
    $display_repository->getFormDisplay('node', $this->nodeTypeWithTags
        ->id())
        ->setComponent($this->tagFieldName, [
        'type' => 'entity_reference_autocomplete_tags',
    ])
        ->save();
    $display_repository->getViewDisplay('node', $this->nodeTypeWithTags
        ->id())
        ->setComponent($this->tagFieldName, [
        'type' => 'entity_reference_label',
        'weight' => 10,
    ])
        ->save();
    $display_repository->getViewDisplay('node', $this->nodeTypeWithTags
        ->id(), 'teaser')
        ->setComponent('field_views_testing_tags', [
        'type' => 'entity_reference_label',
        'weight' => 10,
    ])
        ->save();
}

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