You are here

public function HideableBoolean::viewElements in farmOS 2.x

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

File

modules/core/field/src/Plugin/Field/FieldFormatter/HideableBoolean.php, line 62

Class

HideableBoolean
Plugin implementation of the 'hideable_boolean' formatter.

Namespace

Drupal\farm_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $formats = $this
    ->getOutputFormats();
  foreach ($items as $delta => $item) {
    $format = $this
      ->getSetting('format');
    $hide_if_true = (bool) $this
      ->getSetting('hide_if_true');
    $hide_if_false = (bool) $this
      ->getSetting('hide_if_false');

    // If the item should be hidden, skip it.
    if ($item->value && $hide_if_true || !$item->value && $hide_if_false) {
      continue;
    }
    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;
}