You are here

CpfDigitsFormatter.php in CPF 8

Same filename and directory in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/CpfDigitsFormatter.php

File

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

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_digits' formatter.
 *
 * @FieldFormatter(
 *   id = "cpf_digits",
 *   label = @Translation("Only digits"),
 *   field_types = {
 *     "cpf"
 *   }
 * )
 */
class CpfDigitsFormatter 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')
      ->digits($item->value);
  }

}

Classes

Namesort descending Description
CpfDigitsFormatter Plugin implementation of the 'cpf_digits' formatter.