CpfDigitsFormatter.php in CPF 8
Same filename and directory in other branches
Namespace
Drupal\cpf\Plugin\Field\FieldFormatterFile
src/Plugin/Field/FieldFormatter/CpfDigitsFormatter.phpView 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
Name | Description |
---|---|
CpfDigitsFormatter | Plugin implementation of the 'cpf_digits' formatter. |