You are here

NifFormatter.php in Field NIF 8

File

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

namespace Drupal\field_nif\Plugin\Field\FieldFormatter;

use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'nif_default' formatter.
 *
 * @FieldFormatter(
 *   id = "nif_default",
 *   label = @Translation("NIF/CIF/NIE Formatter"),
 *   field_types = {
 *     "nif"
 *   }
 * )
 */
class NifFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#markup' => $this
          ->viewValue($item),
      ];
    }
    return $elements;
  }

  /**
   * Generate the output appropriate for one field item.
   *
   * @param \Drupal\Core\Field\FieldItemInterface $item
   *   One field item.
   *
   * @return string
   *   The textual output generated.
   */
  protected function viewValue(FieldItemInterface $item) {

    // The text value has no text format assigned to it, so the user input
    // should equal the output, including newlines.
    return Html::escape($item->value);
  }

}

Classes

Namesort descending Description
NifFormatter Plugin implementation of the 'nif_default' formatter.