You are here

protected function DynamicEntityReferenceFormatterTest::buildRenderArray in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DynamicEntityReferenceFormatterTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceFormatterTest::buildRenderArray()

Sets field values and returns a render array as built by field list 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.

See also

\Drupal\Core\Field\FieldItemListInterface::view()

3 calls to DynamicEntityReferenceFormatterTest::buildRenderArray()
DynamicEntityReferenceFormatterTest::testEntityFormatter in tests/src/Kernel/DynamicEntityReferenceFormatterTest.php
Tests the entity formatter.
DynamicEntityReferenceFormatterTest::testIdFormatter in tests/src/Kernel/DynamicEntityReferenceFormatterTest.php
Tests the ID formatter.
DynamicEntityReferenceFormatterTest::testLabelFormatter in tests/src/Kernel/DynamicEntityReferenceFormatterTest.php
Tests the label formatter.

File

tests/src/Kernel/DynamicEntityReferenceFormatterTest.php, line 396

Class

DynamicEntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

protected function buildRenderArray(array $referenced_entities, $formatter, array $formatter_options = []) {

  // Create the entity that will have the entity reference field.
  $referencing_entity = EntityTest::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,
  ]);
}