You are here

public function EntityReferenceDisplayFormatterTrait::viewElements in Entity Reference Display 8

File

src/Plugin/Field/FieldFormatter/EntityReferenceDisplayFormatterTrait.php, line 64

Class

EntityReferenceDisplayFormatterTrait
Plugin trait for the 'entity_reference_display' formatters.

Namespace

Drupal\entity_reference_display\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  // Get display mode fields.
  $display_fields = $this
    ->getDisplayFields();

  // Get selected display mode field.
  $display_field = $this
    ->getDisplayField($display_fields);

  /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */
  $entity = $items
    ->getEntity();

  // Only if entity has this field.
  if ($entity
    ->hasField($display_field)) {

    // Get selected display mode value from field.
    $display_mode = $entity
      ->get($display_field)->value;

    // Only if some value available.
    if (!empty($display_mode)) {

      // Override display mode setting.
      $this
        ->setSetting('view_mode', $display_mode);
    }
  }

  // Use parent method for rendering.
  return parent::viewElements($items, $langcode);
}