You are here

BooleanExportFormatter.php in REST Views 2.0.x

Same filename and directory in other branches
  1. 8 src/Plugin/Field/FieldFormatter/BooleanExportFormatter.php

File

src/Plugin/Field/FieldFormatter/BooleanExportFormatter.php
View source
<?php

namespace Drupal\rest_views\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\rest_views\SerializedData;

/**
 * Export a boolean as a serialized value.
 *
 * @FieldFormatter(
 *   id = "boolean_export",
 *   label = @Translation("Export boolean"),
 *   field_types = {
 *     "boolean",
 *   }
 * )
 */
class BooleanExportFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) : array {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#type' => 'data',
        '#data' => SerializedData::create((bool) $item->value),
      ];
    }
    return $elements;
  }

}

Classes

Namesort descending Description
BooleanExportFormatter Export a boolean as a serialized value.