You are here

public function EntityReferenceLayoutFormatter::viewElements in Entity Reference with Layout 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides EntityReferenceRevisionsEntityFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/EntityReferenceLayoutFormatter.php, line 78

Class

EntityReferenceLayoutFormatter
Entity Reference with Layout field formatter.

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $elements['#theme'] = 'entity_reference_layout';
  $elements['#elements']['field_name'] = $this->fieldDefinition
    ->getName();
  $elements['#elements']['#view_mode'] = $this
    ->getSetting('view_mode');
  try {

    /* @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items */
    $elements['#content'] = $this
      ->buildLayoutTree($items, $langcode);
  } catch (\Exception $e) {
    watchdog_exception('Erl formatter, build layout tree', $e);
  }
  return $elements;
}