function EntityReferenceFormatterTest::buildRenderArray

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

Sets field values and returns a render array.

The render array structure is as built by \Drupal\Core\Field\FieldItemListInterface::view().

Parameters

\Drupal\Core\Entity\EntityInterface[] $referenced_entities: An array of entity objects that will be referenced.

string $formatter: The formatted plugin that will be used for building the render array.

array $formatter_options: Settings specific to the formatter. Defaults to the formatter's default settings.

Return value

array A render array.

4 calls to EntityReferenceFormatterTest::buildRenderArray()
EntityReferenceFormatterTest::testCustomCacheTagFormatter in core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php
Tests the merging of cache metadata.
EntityReferenceFormatterTest::testEntityFormatter in core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php
Tests the entity formatter.
EntityReferenceFormatterTest::testIdFormatter in core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php
Tests the ID formatter.
EntityReferenceFormatterTest::testLabelFormatter in core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php
Tests the label formatter.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php, line 427

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

protected function buildRenderArray(array $referenced_entities, $formatter, $formatter_options = []) {
    // Create the entity that will have the entity reference field.
    $referencing_entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityType)
        ->create([
        'name' => $this->randomMachineName(),
    ]);
    $items = $referencing_entity->get($this->fieldName);
    // Assign the referenced entities.
    foreach ($referenced_entities as $referenced_entity) {
        $items[] = [
            'entity' => $referenced_entity,
        ];
    }
    // Build the renderable array for the field.
    return $items->view([
        'type' => $formatter,
        'settings' => $formatter_options,
    ]);
}

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