You are here

public function BooleanFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter::viewElements()

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 FormatterInterface::viewElements

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/BooleanFormatter.php, line 119
Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\BooleanFormatter.

Class

BooleanFormatter
Plugin implementation of the 'boolean' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $formats = $this
    ->getOutputFormats();
  foreach ($items as $delta => $item) {
    $format = $this
      ->getSetting('format');
    if ($format == 'custom') {
      $elements[$delta] = [
        '#markup' => $item->value ? $this
          ->getSetting('format_custom_true') : $this
          ->getSetting('format_custom_false'),
      ];
    }
    else {
      $elements[$delta] = [
        '#markup' => $item->value ? $formats[$format][0] : $formats[$format][1],
      ];
    }
  }
  return $elements;
}