function FieldAttachOtherTest::testEntityDisplayViewMultiple

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayViewMultiple()
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayViewMultiple()
  3. 11.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityDisplayViewMultiple()

Tests rendering fields with EntityDisplay::buildMultiple().

File

core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php, line 140

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testEntityDisplayViewMultiple() : void {
    // Use a formatter that has a prepareView() step.
    $display = \Drupal::service('entity_display.repository')->getViewDisplay('entity_test', 'entity_test', 'full')
        ->setComponent($this->fieldTestData->field_name, [
        'type' => 'field_test_with_prepare_view',
    ]);
    // Create two entities.
    $entity1 = EntityTest::create([
        'id' => 1,
        'type' => 'entity_test',
    ]);
    $entity1->{$this->fieldTestData->field_name}
        ->setValue($this->_generateTestFieldValues(1));
    $entity2 = EntityTest::create([
        'id' => 2,
        'type' => 'entity_test',
    ]);
    $entity2->{$this->fieldTestData->field_name}
        ->setValue($this->_generateTestFieldValues(1));
    // Run buildMultiple(), and check that the entities come out as expected.
    $display->buildMultiple([
        $entity1,
        $entity2,
    ]);
    $item1 = $entity1->{$this->fieldTestData->field_name}[0];
    $this->assertEquals($item1->value + 1, $item1->additional_formatter_value, 'Entity 1 ran through the prepareView() formatter method.');
    $item2 = $entity2->{$this->fieldTestData->field_name}[0];
    $this->assertEquals($item2->value + 1, $item2->additional_formatter_value, 'Entity 2 ran through the prepareView() formatter method.');
}

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