You are here

CpfMaskFormatter.php in CPF 8.2

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

File

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

/**
 * @file
 * Contains \Drupal\cpf\Plugin\Field\FieldFormatter\CpfMaskFormatter.
 */
namespace Drupal\cpf\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;

/**
 * Plugin implementation of the 'cpf_mask' formatter.
 *
 * @FieldFormatter(
 *   id = "cpf_mask",
 *   label = @Translation("Masked"),
 *   field_types = {
 *     "cpf"
 *   }
 * )
 */
class CpfMaskFormatter 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) {
    return \Drupal::service('cpf')
      ->mask($item->value);
  }

}

Classes

Namesort descending Description
CpfMaskFormatter Plugin implementation of the 'cpf_mask' formatter.